Java - 可选循环

Java - Optional loop

这是我的代码:

/* Linear equation student quiz
 * This program creates equations of the form ax + b = c for students to solve.
 */

import java.util.Random;
import java.util.Scanner;

public class MathFunction {
    public static void main(String[] args) {
        int a, b, c;

        double userAnswer, correctAnswer;
        int numCorrect = 0;
        Random ranNum = new Random();
        Scanner input = new Scanner(System.in);

        for (int problem = 1; problem <= 10; problem++)
        {
            a = ranNum.nextInt(2) + 1;
            b = ranNum.nextInt(41) - 20;
            c = ranNum.nextInt(41) - 20;

            System.out.print("\n"+ a + "x + " + b + " = " + c + " ... x = ");

            userAnswer = input.nextDouble();
            correctAnswer = 1.0 * (c - b) / a;

            if (userAnswer == correctAnswer)
            {
                System.out.println("Correct!");
                numCorrect =+ 1;
            }
            else
            {
                System.out.println("Sorry, correct answer is " + correctAnswer);
            }

        }//end for loop
        System.out.println("You got " + numCorrect + " out of ten.");
        System.out.println("\nWant to do 10 more questions? <y/n>");

    }//end main
}//end class

如果用户输入字符 'y',我希望能够 return 进入循环。每当用户完成 10 道数学题时,系统都会提示用户选择此选项。我会使用 'do-while' 吗?

是的,您应该用一个 do-while 循环包装 for 循环,检查用户是否输入了 'y'。

do {
    for (...) {
        ...
    }
    System.out.println("You got " + numCorrect + " out of ten.");
    System.out.println("\nWant to do 10 more questions? <y/n>");
    input.nextLine();
    String repeat = input.nextLine();
} while (repeat.equals("y"));

这是简单的解决方案

String choice = "y";
while(choice.equals("y")){
for (int problem = 1; problem <= 10; problem++)
    {
        a = ranNum.nextInt(2) + 1;
        b = ranNum.nextInt(41) - 20;
        c = ranNum.nextInt(41) - 20;



System.out.print("\n"+ a + "x + " + b + " = " + c + " ... x = ");

    userAnswer = input.nextDouble();
    correctAnswer = 1.0 * (c - b) / a;

    if (userAnswer == correctAnswer)
    {
        System.out.println("Correct!");
        numCorrect =+ 1;
    }
    else
    {
        System.out.println("Sorry, correct answer is " + correctAnswer);
    }

}//end for loop
System.out.println("You got " + numCorrect + " out of ten.");
System.out.println("\nWant to do 10 more questions? <y/n>");
choice = input.nextLine(); // get the input
}

-> 将您的代码包装在一个 do while 循环中。

-> 还使用 input.nextLine() 读取所有用户输入(双精度值和字符串 "y" 或 "n"),作为在 input.nextDouble() 和 [=11= 之间切换] ,有时会导致错误。用户输入后将输入值解析为double

 outer: //label
 do{
 for (int problem = 1; problem <= 10; problem++)
{
    a = ranNum.nextInt(2) + 1;
    b = ranNum.nextInt(41) - 20;
    c = ranNum.nextInt(41) - 20;

    System.out.print("\n"+ a + "x + " + b + " = " + c + " ... x = ");

    try{
    userAnswer = Double.parseDouble(input.nextLine()); //use this to get double input from user
    }
    catch(NumberFormatException e){
   //warn user of wrong input 
    break outer;
    }

    correctAnswer = 1.0 * (c - b) / a;

    if (userAnswer == correctAnswer)
    {
        System.out.println("Correct!");
        numCorrect =+ 1;
    }
    else
    {
        System.out.println("Sorry, correct answer is " + correctAnswer);
    }

}//end for loop
System.out.println("You got " + numCorrect + " out of ten.");
System.out.println("\nWant to do 10 more questions? <y/n>");

if(input.nextLine().equalsIgnoreCase("y")){
continue outer; //if user wants to continue
 }
else{
break outer; //if user does not want to continue, break out of outer do-while loop
}

}
while(true);

这就是我所说的将程序分解成方法的意思,

/* 线性方程学生测验 * 该程序创建形式为 ax + b = c 的方程供学生求解。 */

import java.util.Random;
import java.util.Scanner;

public class MathFunction {

    int a, b, c;
    double userAnswer, correctAnswer;
    int numCorrect = 0; 
    Random ranNum = new Random();
    Scanner input = new Scanner(System.in);
    //You function to calculate
    public static compute()
    {
     for (int problem = 1; problem <= 10; problem++)
        {
            a = ranNum.nextInt(2) + 1;
            b = ranNum.nextInt(41) - 20;
            c = ranNum.nextInt(41) - 20;

            System.out.print("\n"+ a + "x + " + b + " = " + c + " ... x = ");

            userAnswer = input.nextDouble();
            correctAnswer = 1.0 * (c - b) / a;

            if (userAnswer == correctAnswer)
            {
                System.out.println("Correct!");
                numCorrect =+ 1;
            }
            else
            {
                System.out.println("Sorry, correct answer is " + correctAnswer);
            }

        }//end for loop
         System.out.println("You got " + numCorrect + " out of ten.");
         System.out.println("\nWant to do 10 more questions? <y/n>");

    }

    public static void main(String[] args) {
        // Then start by sking a question like "Ready to staxt Y/N"

        //get user responce or user input and if user input is Y then call the compute method else  system exit.

        if(userAnswer=="Y")
        {
            compute();
        }
        else{
            //Thanks for participating system closes.
            System.exit(0);
        }



    }//end main
}//end class