Java 数学加法代码,尝试在正确答案后结束

Java 数学加法代码,尝试在正确答案后结束,java,Java,我的问题是,在得到正确答案后,它仍然会问问题。我错过了什么?我现在还是JAVA新手,不想因为一个简单的错误或疏忽而气馁 package acourtney; import java.util.Scanner; public class Hmwk03 { public static void main(String[] args) { int number1 = (int)(Math.random() * 10); int number2 = (int)(Math.rando

我的问题是,在得到正确答案后,它仍然会问问题。我错过了什么?我现在还是JAVA新手,不想因为一个简单的错误或疏忽而气馁

package acourtney;

import java.util.Scanner;

public class Hmwk03 {

public static void main(String[] args) {
    int number1 = (int)(Math.random() * 10);
    int number2 = (int)(Math.random() * 10);
    int counter = 0;

    Scanner input =new Scanner(System.in);

    System.out.print ("What is " + number1 + " + " + number2 + " ? " + "(You have three chances) ");

    int answer = input.nextInt();

        if (number1 + number2 == answer)
            System.out.println("Answer is correct");

        else
            System.out.println("Your answer is incorrect. You have two chances left ");
            counter = 2; 

    System.out.print("What is " + number1 + " + " + number2 + " ? ");

    answer = input.nextInt();

        if (number1 + number2 == answer)

            System.out.println("Answer is correct");

        else
            System.out.println("Your answer is incorrect. You have one chance left");
        counter = 3;

        System.out.print("What is " + number1 + " + " + number2 + " ? ");

        answer = input.nextInt();

        if (number1 + number2 == answer)
            System.out.println("Answer is correct");

        else
            System.out.println("Your answer is incorrect, You have failed");

        }


}

每次回答正确后退出程序

if (number1 + number2 == answer){
            System.out.println("Answer is correct");
System.exit(0);
}

每次回答正确后退出程序

if (number1 + number2 == answer){
            System.out.println("Answer is correct");
System.exit(0);
}

您可以使用
return
语句提前结束方法

if (number1 + number2 == answer) {
    System.out.println("Answer is correct");
    return;
}

您可以使用
return
语句提前结束方法

if (number1 + number2 == answer) {
    System.out.println("Answer is correct");
    return;
}

你的问题是你在执行

if(number1+number2==答案)
System.out.println(“答案正确”)

然后跳过

else
System.out.println(“您的答案不正确,您还有两次机会”)

然后在
计数器=2处重新拾取右键


如果您编写的If/else子句包含多行代码(例如,在输入不正确的语句后所发生的一切),则必须在整个语句周围加上大括号
{}
。最好的做法是使用大括号,即使子句只有一行。

您的问题是您正在执行

if(number1+number2==答案)
System.out.println(“答案正确”)

然后跳过

else
System.out.println(“您的答案不正确,您还有两次机会”)

然后在
计数器=2处重新拾取右键


如果您编写的If/else子句包含多行代码(例如,在输入不正确的语句后所发生的一切),则必须在整个语句周围加上大括号
{}
。最佳实践是,即使子句只有一行,也要使用大括号。

因此Java将逐行运行,这里您看到的是,一旦用户应答,程序将退出if语句,并继续执行下一个if语句

正如

 int answer = input.nextInt();

    if (number1 + number2 == answer)
        System.out.println("Answer is correct");

    else
        System.out.println("Your answer is incorrect. You have two chances left ");
        counter = 2; 

System.out.print("What is " + number1 + " + " + number2 + " ? ");
为了解决这个问题(以及您似乎有复制和粘贴代码的问题,这从来都不是一个好的做法),我建议按照

//int numOfTries is how many chances you want to give the user
for(int i = 0; i < numOfTries; i++){
    // Prompt the user for Input
    int answer = input.nextInt();

    // Check if the answer is correct
    if (number1 + number2 == answer){
        System.out.println("Answer is correct");
        //Now exit the for loop, we are done!
        return;
    }
    // What happens if the answer is not correct
    else{
        // Print out how many tries they have left as well
        System.out.println("Your answer is incorrect. You have" + i + "chance left");
    }
}
//int numotries是您希望给用户多少机会
for(int i=0;i
希望这有帮助


p.S.而不是<代码>中断
您也可以使用
return
系统退出(0)

因此Java将逐行运行,这里您看到的是,一旦用户应答,程序就会退出if语句,并继续向下一个if语句前进

正如

 int answer = input.nextInt();

    if (number1 + number2 == answer)
        System.out.println("Answer is correct");

    else
        System.out.println("Your answer is incorrect. You have two chances left ");
        counter = 2; 

System.out.print("What is " + number1 + " + " + number2 + " ? ");
为了解决这个问题(以及您似乎有复制和粘贴代码的问题,这从来都不是一个好的做法),我建议按照

//int numOfTries is how many chances you want to give the user
for(int i = 0; i < numOfTries; i++){
    // Prompt the user for Input
    int answer = input.nextInt();

    // Check if the answer is correct
    if (number1 + number2 == answer){
        System.out.println("Answer is correct");
        //Now exit the for loop, we are done!
        return;
    }
    // What happens if the answer is not correct
    else{
        // Print out how many tries they have left as well
        System.out.println("Your answer is incorrect. You have" + i + "chance left");
    }
}
//int numotries是您希望给用户多少机会
for(int i=0;i
希望这有帮助


p.S.而不是<代码>中断
您也可以使用
return
系统退出(0)

作为一名Java初学者,我建议您对程序进行伪代码编写,为自己提供一个蓝图。它可以帮助您将代码分解为更小的步骤

public class Hmwk03 {

public static void main(String[] args) {
    int number1 = (int)(Math.random() * 10);
    int number2 = (int)(Math.random() * 10);
    int counter = 2; // Define the number of chances they have, start at 2 since the first counter value is 0
    boolean answerCheck = false; // Define if they answer was right

    Scanner input =new Scanner(System.in);

    // Generate a for loop with a counter starting at 0
    // The loop will run either if they have more chances left
    // OR when they have gotten the answer right
    for(int i = 0; i < counter || answerCheck == true; i++) {

        System.out.print ("What is " + number1 + " + " + number2 + " ? " + "(You have three chances) "); // Output the question

        int answer = input.nextInt(); // Receive the answer

        if (number1 + number2 == answer) { // if they got the right ansser
            // Make the answerCheck true or use break here                
            answerCheck = true; 
            System.out.println("Answer is correct"); // Output the user got the correct answer
            //break; This will also work to end the loop
        } else { // they got the wrong answer
            System.out.println("Your answer is incorrect. You have " + counter - i + " chances left."); // Note they got the answer wrong and tell them how many chances they have left 
        }
    }
    if(!answerCheck) System.out.print(" You have failed"); // If they failed to get the answer correct, print out final statement.

}
公共类Hmwk03{
公共静态void main(字符串[]args){
int number1=(int)(Math.random()*10);
int number2=(int)(Math.random()*10);
int counter=2;//定义它们的机会数,从2开始,因为第一个计数器值是0
boolean answerCheck=false;//定义他们的答案是否正确
扫描仪输入=新扫描仪(System.in);
//生成计数器从0开始的for循环
//如果他们还有更多的机会,循环将运行
//或者当他们得到正确的答案时
for(int i=0;i
作为Java的初学者,我建议您对程序进行伪编码,为自己绘制蓝图。它可以帮助您将代码分解为更小的步骤

public class Hmwk03 {

public static void main(String[] args) {
    int number1 = (int)(Math.random() * 10);
    int number2 = (int)(Math.random() * 10);
    int counter = 2; // Define the number of chances they have, start at 2 since the first counter value is 0
    boolean answerCheck = false; // Define if they answer was right

    Scanner input =new Scanner(System.in);

    // Generate a for loop with a counter starting at 0
    // The loop will run either if they have more chances left
    // OR when they have gotten the answer right
    for(int i = 0; i < counter || answerCheck == true; i++) {

        System.out.print ("What is " + number1 + " + " + number2 + " ? " + "(You have three chances) "); // Output the question

        int answer = input.nextInt(); // Receive the answer

        if (number1 + number2 == answer) { // if they got the right ansser
            // Make the answerCheck true or use break here                
            answerCheck = true; 
            System.out.println("Answer is correct"); // Output the user got the correct answer
            //break; This will also work to end the loop
        } else { // they got the wrong answer
            System.out.println("Your answer is incorrect. You have " + counter - i + " chances left."); // Note they got the answer wrong and tell them how many chances they have left 
        }
    }
    if(!answerCheck) System.out.print(" You have failed"); // If they failed to get the answer correct, print out final statement.

}
公共类Hmwk03{
公共静态void main(字符串[]args){
int number1=(int)(Math.random()*10);