Java 掷骰子游戏

Java 掷骰子游戏,java,validation,while-loop,character,dice,Java,Validation,While Loop,Character,Dice,因此,在这个掷骰子游戏中,用户输入的内容必须遵循xdy的格式,其中“x”是骰子的数量,“y”是骰子的边数。如果输入不符合格式,程序应要求用户输入其他输入 public static void main(String[] args) { // TODO Auto-generated method stub Scanner user_input = new Scanner(System.in); String input; System.out.println("

因此,在这个掷骰子游戏中,用户输入的内容必须遵循xdy的格式,其中“x”是骰子的数量,“y”是骰子的边数。如果输入不符合格式,程序应要求用户输入其他输入

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner user_input = new Scanner(System.in);

    String input;
    System.out.println("Please enter the number and type of dice to roll in the format <number>d<sides>.");
    input = user_input.nextLine();  

    while()
            {
            System.out.println("Input is not valid. Please enter a new input");
            input = user_input.nextLine();
            }

    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(5);
publicstaticvoidmain(字符串[]args){
//TODO自动生成的方法存根
扫描仪用户输入=新扫描仪(System.in);
字符串输入;
System.out.println(“请以d.格式输入要掷骰子的数量和类型”);
input=user_input.nextLine();
while()
{
System.out.println(“输入无效,请输入新输入”);
input=user_input.nextLine();
}
Random randomGenerator=新的Random();
int randomInt=randomGenerator.nextInt(5);

我的问题是,如何在循环检查两个整数之间的字符时执行


将numberOfSides和dice的数量分成两个变量,然后对它们执行任何操作

String input="6d3";
int numSides=Integer.parseInt(input.split("d")[0]);
int numDice=Integer.parseInt(input.split("d")[1]);

如果需要9个以上的骰子和/或9个以上边的骰子怎么办?
\\d+d\\d+
不是更好的正则表达式吗?另外,为什么不使用
模式
来捕获骰子的数量和骰子的边数?@bobulus很好,谢谢!按照您的建议进行编辑和改进。
String input="6d3";
int numSides=Integer.parseInt(input.split("d")[0]);
int numDice=Integer.parseInt(input.split("d")[1]);