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

时间:2017-02-16 01:44:45

标签: java

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

/* phi - field over which derivatives are calculated */
float *phi = (float *) calloc(SIZE, sizeof(float));

/* rhs - derivatives in each direction are summed and
         stored in this variable 
*/
float *rhs = (float *) calloc(SIZE, sizeof(float));

5 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

作为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.

}

答案 2 :(得分:0)

每个正确答案后退出程序。

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

答案 3 :(得分:0)

您的问题是您正在执行

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再次向右挑选。

如果您正在编写一个if / else子句,其中包含多行代码(例如,在您输入一个不正确的语句之后发生的所有内容),您必须放置卷曲整个事情围绕{}。最佳做法是使用花括号,即使该子句只有一行。

答案 4 :(得分:0)

所以Java将逐行运行,你在这里得到的是,一旦用户回答,程序就会退出if语句,然后继续向下发送if语句 < / p>

所示

 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");
    }
}

希望这有帮助!

P.S。您可以使用break;return;

代替System.exit(0);