2017-02-16 41 views
0

我遇到的問題是,在正確的答案後,它仍然問這個問題。我錯過了什麼?我現在對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"); 

     } 


} 
+0

你看過關於在Java中猜測數字的其他問題嗎?或者你是否至少用調試器遍歷了你的代碼? –

+2

總是加上大括號'{}'。不要因爲沒有必要就忽視它們。 – Carcigenicate

+0

@Carcigenicate這是一種個人風格的選擇。 – weston

回答

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. 

} 
+0

我原本遞減計數器以顯示system.out.println中左轉的次數,但我的意思是顯示counter-i。 i值會隨着循環的每一次增加而增加,因此例如第一次運行i將是0,所以反向 - i - >(2 - 0)2機會離開,第二次運行i = 1,counter - i(2 - 1)剩下1次機會等等 –

0

每次正確答案後退出程序。

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

您可以儘早用return語句結束方法。

if (number1 + number2 == answer) { 
    System.out.println("Answer is correct"); 
    return; 
} 
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子句(比如說輸入一個不正確的語句後發生的所有事情),你必須在整個事物上放置大括號{}。最佳做法是即使在子句只有一行的情況下也使用大括號。

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

希望這有助於!

P.S.而不是break;你也可以使用return;System.exit(0);

+0

當我將代碼更改爲您的建議時,它告訴我「break」不能在循環或開關外使用。我使用eclipse來運行代碼,如果這有幫助 –

+0

嘿,在這種情況下,只需使用「返回」;這應該能解決你的問題。我相應地編輯了我的解決方案 –