Dicecode RPG , Java. What's wrong with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Dicecode RPG , Java. What's wrong with this code?

Hello, please could you help me with this code? I am trying to set up a method that reads input from the user, a text String specifically coded to include the number of dice tosses, type of dice and the modifier, eg. 3D8+5 , 2D20-2, D6-1, 3D100-15, etc..but I can't exit the while(true) loop assuming that the string text is writed properly at least having one of the dice type included, here' s the code. thank you in advance. import java.util.Random; import java.util.Scanner; public class rpgDiceGame { static final String[] DICE = {"D3","D4","D6","D8","D10","D12","D20","D100"}; public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please input your dice code string: "); String diceCode = scan.next(); chkCode(diceCode); } static int chkCode (String input) { while(getString(input)); String[] diceParts = (input.split("D|\\+\\-")); int toss = Integer.parseInt(diceParts[0]); int diceType = Integer.parseInt(diceParts[1]); if (input.contains("-")) { int modifier = -(Integer.parseInt(diceParts[2])); return (number(diceType, toss) + modifier); } else { int modifier = Integer.parseInt(diceParts[2]); return (number(diceType, toss) + modifier); } } static boolean getString (String str) { boolean result = false; for (String x : DICE) { if (!str.contains(x)) { result = true; } } return result; } static int number(int num1, int num2) { int sum = 0; Random roll = new Random(); for (int i = 0; i < num2; i

28th Dec 2018, 2:07 PM
Roberto
1 Answer
0
The method didn't paste completely: number(int num1, int num2){ Int sum=0; Random roll = new Random(); For(int i=0;i<num 2;i++) { Int result = roll.nextInt(num1)+1; sum+=result; } return sum; }
28th Dec 2018, 2:47 PM
Roberto