Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在X+;Y=Z操作和循环_Java_Loops_Variables_Random - Fatal编程技术网

Java 如何在X+;Y=Z操作和循环

Java 如何在X+;Y=Z操作和循环,java,loops,variables,random,Java,Loops,Variables,Random,我基本上希望能够循环一个X+Y=Z方程,直到用户输入除整数以外的其他值,比如字母“A”,并且当有任何数字时,循环停止显示消息 此外,我对如何随机定位用户必须输入正确答案的“?”感到困惑 比如说 System.out.println("What is: " + num1 + " + ? = " + answer); 到目前为止: 我正在通过IF语句手动定位“?”。这能以更有效的方式完成吗 public static void main(String[] args) { Random

我基本上希望能够循环一个X+Y=Z方程,直到用户输入除整数以外的其他值,比如字母“A”,并且当有任何数字时,循环停止显示消息

此外,我对如何随机定位用户必须输入正确答案的“?”感到困惑

比如说

System.out.println("What is: " + num1 + " + ? = " + answer);
到目前为止:

我正在通过IF语句手动定位“?”。这能以更有效的方式完成吗

public static void main(String[] args) {


    Random rand = new Random();

    Scanner input = new Scanner(System.in);

    int num1, num2, number3, answer;


    do {

        num1= 1 + rand.nextInt(10);
        num2= 1 + rand.nextInt(10);

        answer= num1 + num2;
        System.out.println("What is: " + num1 + " + ? = " + answer);

        number3= input.nextInt();

        if (number3 == num2)
            System.out.println("That is correct");
        else 
            System.out.println("That is wrong");

        num1= 1 + rand.nextInt(10);
        num2= 1 + rand.nextInt(10);

        answer= num1 + num2;

        System.out.println(num1 + " + ? = " + answer);

        number3= input.nextInt();

    } while(number3 !=0);       
}

以下是一种方法:

public static void main(String[] args) {

    Random rand = new Random();
    Scanner scanner = new Scanner(System.in);
    int[] xyz = new int[3];
    String[] display = new String[3];
    int answer, position;

    do {
        xyz[0] = 1 + rand.nextInt(10);
        xyz[1] = 1 + rand.nextInt(10);
        xyz[2] = xyz[0] + xyz[1];
        for (int i = 0; i < xyz.length; i++)
            display[i] = String.valueOf(xyz[i]);
        position = rand.nextInt(3);
        display[position] = "?";
        System.out.println("What is: " + display[0] + " + " + display[1] + " = " + display[2]);

        do {
            System.out.println("Please enter an integer or 'S' to stop");
            String input = scanner.nextLine();
            if (input.equals("S")) {
                scanner.close();
                System.out.println("Stopped");
                return;
            }
            else if (input.matches("\\d+")) { // \\d+ means "a digit (0-9) once or more
                answer = Integer.parseInt(input);
                break;
            }
        } while (true);

        if (answer == xyz[position])
            System.out.println("That is correct");
        else
            System.out.println("That is wrong");
    } while (true);
}
publicstaticvoidmain(字符串[]args){
Random rand=新的Random();
扫描仪=新的扫描仪(System.in);
int[]xyz=新的int[3];
字符串[]显示=新字符串[3];
回答,位置;
做{
xyz[0]=1+rand.nextInt(10);
xyz[1]=1+rand.nextInt(10);
xyz[2]=xyz[0]+xyz[1];
对于(int i=0;i
注意事项:

  • 我使用内部
    do while
    循环反复检查输入,并要求用户输入有效的输入
  • 我使用两个数组:一个用于存储数字,另一个用于存储显示值
  • 我还添加了一个停止条件,因为外循环是无限的。完成后始终关闭
    扫描仪
  • 我为“?”随机选择了3个位置中的1个

您是否尝试过try/catch块来检测非整数输入?例如,try{number3=input.nextInt();}catch(异常e){break;}我还没有测试过它,但是它看起来是一种检测无效输入的好方法。看起来你做了同样的事情两次。代码看起来像是在你的.if语句之后重复它。也许考虑删除这个。你能详细说明“我正在定位”吗?“手动通过IF语句。”?“手动通过IF语句。不,您不是,您正将打印语句定位在它的位置。基本上你想要的是检查输入是否是任何整数?@Tuan333我将尝试使用catch/try,谢谢@聪明的trevor很抱歉造成混淆,我希望能够更改用户看到“?”的位置。有什么想法吗?谢谢你的意见!然而,由于我还没有真正学会如何尝试,我正在寻找其他方法来解决这个问题。一个想法是,我不知道这是否可行。我在考虑创建一个do-while循环,在这个循环中有一个if语句不断地检查不是整数的答案。还有一个键盘输入,可以打印特定的信息。我是编程新手,不知道是否有这样的语法可用。例如:如果user.input!=整数;println(“请输入一个整数”)@Tor我看不出这与“如何在X+Y=Z操作和循环中随机定位变量”的问题有什么关系。你必须非常清楚你到底想要什么。你也必须告诉我们你能用什么。