Я пытаюсь сделать игру в кости TTRPG с помощью Java в приложении Java FX. Пока что у меня так: программа пропускает блок try и ничего не показывает на экране, консоль ошибок не показывает. Вы можете помочь мне? Я пишу это для личного проекта. Я тоже студент, поэтому мало что знаю о программировании и стараюсь максимально избегать использования ИИ.
public class DiceController {
@FXML
Label rollResult, separatedDice;
@FXML
TextField diceNotationField;
@FXML
Button diceRollButton;
public void roll() {
try {
// get text from field
String diceNotation = diceNotationField.getText();
// Use a regex tp detect dice notation
Pattern regex = Pattern.compile("(\\d+)?d(\\d+)(?:([\\+\\*\\/\\-])(\\d+))?");
Matcher m = regex.matcher(diceNotation);
String diceQuantity = "";
String diceSize = "";
String diceSign = "";
String diceModifier = "";
// Call Random to create a random number
Random rand = new Random();
// if the dice quantity is null is set to 1
if (diceQuantity == null || diceQuantity.isEmpty()) {
diceQuantity = "1";
}
// regex matcher check
if (!m.matches()) {
rollResult.setText("Intruduce a valid dice notation");
} else if (m.matches()) {
diceQuantity = m.group(1);
diceSize = m.group(2);
// Create an array to store the result of each dice
int diceResultArray[] = new int[Integer.parseInt(diceQuantity)];
// this variable will generate a random number rolling the dice
int randomResultInDice = 0;
// this variable stores the total result
int total = 0;
for (int i = 0; i < Integer.parseInt(diceQuantity); i++) {
// generates a random number on the dice,
randomResultInDice = rand.nextInt(1, Integer.parseInt(diceSize) + 1);
// stores the separate values of each dice's results in an array
diceResultArray = randomResultInDice;
// sums the result of the separate dice
total += randomResultInDice;
}
// if there's no sign the string stays empty, else, the regex group is
// recognized and the corresponding operation is performed
if (diceSign == null || diceSign.isEmpty() && diceModifier == null || diceModifier.isEmpty()) {
diceSign = "";
diceModifier = "";
} else {
diceSign = m.group(3);
diceModifier = m.group(4);
// stores the total result of the dice plus the operation corresponding to the
// modifier
int totalMod = 0;
switch (diceSign) {
case "+":
totalMod = total + Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
case "-":
totalMod = total - Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
case "*":
totalMod = total * Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
case "/":
if (Integer.parseInt(diceModifier) != 0) {
totalMod = total / Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
} else {
totalMod = total;
rollResult.setText(String.valueOf(total));
}
break;
default:
rollResult.setText(String.valueOf(total));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
}
}
}
} catch (Exception e) {
rollResult.setText(e.toString());
System.out.println(e);
}
}
}
Файл FXML:
Подробнее здесь: https://stackoverflow.com/questions/798 ... here-are-n
При попытке сделать ролик игры в кости TTRPG программа не отображает содержимое, несмотря на отсутствие ошибок. ⇐ JAVA
Программисты JAVA общаются здесь
1762791696
Anonymous
Я пытаюсь сделать игру в кости TTRPG с помощью Java в приложении Java FX. Пока что у меня так: программа пропускает блок try и ничего не показывает на экране, консоль ошибок не показывает. Вы можете помочь мне? Я пишу это для личного проекта. Я тоже студент, поэтому мало что знаю о программировании и стараюсь максимально избегать использования ИИ.
public class DiceController {
@FXML
Label rollResult, separatedDice;
@FXML
TextField diceNotationField;
@FXML
Button diceRollButton;
public void roll() {
try {
// get text from field
String diceNotation = diceNotationField.getText();
// Use a regex tp detect dice notation
Pattern regex = Pattern.compile("(\\d+)?d(\\d+)(?:([\\+\\*\\/\\-])(\\d+))?");
Matcher m = regex.matcher(diceNotation);
String diceQuantity = "";
String diceSize = "";
String diceSign = "";
String diceModifier = "";
// Call Random to create a random number
Random rand = new Random();
// if the dice quantity is null is set to 1
if (diceQuantity == null || diceQuantity.isEmpty()) {
diceQuantity = "1";
}
// regex matcher check
if (!m.matches()) {
rollResult.setText("Intruduce a valid dice notation");
} else if (m.matches()) {
diceQuantity = m.group(1);
diceSize = m.group(2);
// Create an array to store the result of each dice
int diceResultArray[] = new int[Integer.parseInt(diceQuantity)];
// this variable will generate a random number rolling the dice
int randomResultInDice = 0;
// this variable stores the total result
int total = 0;
for (int i = 0; i < Integer.parseInt(diceQuantity); i++) {
// generates a random number on the dice,
randomResultInDice = rand.nextInt(1, Integer.parseInt(diceSize) + 1);
// stores the separate values of each dice's results in an array
diceResultArray[i] = randomResultInDice;
// sums the result of the separate dice
total += randomResultInDice;
}
// if there's no sign the string stays empty, else, the regex group is
// recognized and the corresponding operation is performed
if (diceSign == null || diceSign.isEmpty() && diceModifier == null || diceModifier.isEmpty()) {
diceSign = "";
diceModifier = "";
} else {
diceSign = m.group(3);
diceModifier = m.group(4);
// stores the total result of the dice plus the operation corresponding to the
// modifier
int totalMod = 0;
switch (diceSign) {
case "+":
totalMod = total + Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
case "-":
totalMod = total - Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
case "*":
totalMod = total * Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
case "/":
if (Integer.parseInt(diceModifier) != 0) {
totalMod = total / Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
} else {
totalMod = total;
rollResult.setText(String.valueOf(total));
}
break;
default:
rollResult.setText(String.valueOf(total));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
}
}
}
} catch (Exception e) {
rollResult.setText(e.toString());
System.out.println(e);
}
}
}
Файл FXML:
Подробнее здесь: [url]https://stackoverflow.com/questions/79815800/trying-to-make-a-ttrpg-dice-roller-program-shows-no-content-despite-there-are-n[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия