Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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 在正确回答第一个问题后,如何循环到新的随机生成?_Java - Fatal编程技术网

Java 在正确回答第一个问题后,如何循环到新的随机生成?

Java 在正确回答第一个问题后,如何循环到新的随机生成?,java,Java,我已经想出了如何让我的程序生成一个随机乘法问题并循环,直到正确回答为止,但是我需要程序在正确回答前一个乘法问题后生成另一个不同的乘法问题…任何提示都将不胜感激 package cai; import java.security.SecureRandom; import java.util.Scanner; public class Cai { public static void main(String[] args) { Scanner input = new Scanner(S

我已经想出了如何让我的程序生成一个随机乘法问题并循环,直到正确回答为止,但是我需要程序在正确回答前一个乘法问题后生成另一个不同的乘法问题…任何提示都将不胜感激

package cai;
import java.security.SecureRandom;
import java.util.Scanner;

public class Cai {


public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int x = 0;
    int y = 0;
    int varFirst = first(x);
    int varSecond = second(y);
    int answer;

    do {
        System.out.printf("What is %d times %d?%n", varFirst, varSecond);
        answer = input.nextInt();
        if (varFirst * varSecond != answer) {
            System.out.printf("No. Please try again%n");
        }
    } while (varFirst * varSecond != answer);
    System.out.printf("Very good!%n");
}
public static int first(int x) {
    SecureRandom randomNumbers = new SecureRandom();
    x = 1 + randomNumbers.nextInt(10);

    return x;
}
public static int second(int y) {
    SecureRandom randomNumbers = new SecureRandom();
    y = 1 + randomNumbers.nextInt(10);

    return y;
}
}

您可以将while循环更改为如下内容

while(true) { System.out.printf("What is %d times %d?%n", varFirst, varSecond); answer = input.nextInt(); if (varFirst * varSecond != answer) { System.out.printf("No. Please try again%n"); return;} }

好吧,既然这个问题已经在评论部分得到了回答,并且OP已经找到了一个有效的解决方案,我想我会有一些的乐趣™并可能提供一些参考点,以促进他对代码质量的学习

这个问题的答案很简单,仅供参考:

再绕一圈


我们开始吧!如果I(有几年Java专业经验,但免责声明:暂时离开它,在没有编辑器的情况下从内存中执行此操作),代码会是什么样子:

public class Cai {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //There is no need for those 'x' and 'y' variables.
        //You're ultimately not using them anyway.

        int first;
        int second;
        int answer;

        //There IS gain in caching result (expected answer) to avoid repeated calculations.
        int expectedAnswer;

        do {
            do {
                //First, second, and expected answer are "part of the question logic".
                //It makes more sense to assign their values here, at start of a new question.
                first = getOperand(10); //One method, used twice.
                second = getOperand(10); //Bonus, now you can choose bound here.
                expectedAnswer = first * second;

                //Question itself; repeat till right answer.
                System.out.printf("What is %d times %d?%n", first, second);
                answer = input.nextInt();
                if (answer != expectedAnswer)
                    System.out.printf("No. Please try again.%n");
            } while (answer != expectedAnswer);
            System.out.printf("Very good!%n");

            //As if user wants another question.
            //Repeat till answer is one that is recognized and can be handled.
            String keepGoing;
            do {
                System.out.printf("Do you want to answer another one? (y|n)%n");
                keepGoing = input.nextLine().trim().toLowerCase();
                if(keepGoing != "y" && keepGoing != "n")
                    System.out.printf("Sorry. I don't understand. I was expecting 'y' or 'n'...%n");
            } while (keepGoing != "y" && keepGoing != "n");
            //Here, answer if be either "y" or "n".
            //If it's "n", then quit the loop with break.
            //If it's not "n", then it's a "y"; do another question.
            if(keepGoing == "n")
                break;
        } while (true);
    }

    //There is no need for two functions performing the same action.
    //Make one method and use it twice instead.
    public static int getOperand(int bound) {
        //This doesn't need cryptographic safety. Normal Random is fine.
        Random rng = new Random(); //rng = Random Number Generator
        return rng.nextInt(bound) + 1;
    }
}

您显然已经知道如何使用循环了。您到底需要什么帮助?只需将您的代码包装到另一个循环中,并询问用户是否希望重新开始。当然不是全部代码。。。我想你可以找到答案,你不需要
SecureRandom
。标准的
Random
就可以了除此之外,你已经学会了如何使用循环,所以答案对你来说应该是显而易见的:用另一个循环来结束。如果每次都有不同的问题很重要,你可以保留一个你已经问过的问题的列表。要解决一个新问题,请不断生成随机数对,直到得到一对不在列表中的随机数。感谢您的帮助,我们能够添加另一个循环以使其正常工作。附加问题:如果用户想要其他问题,我会在每个正确答案后添加什么内容来询问用户?我是在整个过程中使用另一个循环还是if语句的组合?