2017-02-02 33 views
-2

我是新手編程,並希望得到一些幫助。如何執行正確的循環乘法測驗

我正在嘗試創建一個無限循環的乘法測驗,直到用戶輸入no。另外,當出現乘法問題時,程序還必須顯示用戶輸入是否正確。

當用戶輸入no時,循環將停止並顯示詢問的總數量中有多少正確答案。

有什麼想法?

這裏是我的代碼:

public static void main(String[] args) { 
    int correctCount = 0; // count the number correct question 
    int count = 0; // count the number of question 
    Scanner input = new Scanner(System.in); 

    // 2 random single-digit int 
    int number1 = (int) (Math.random() * 10); 
    int number2 = (int) (Math.random() * 10); 

    do { 
    // prompt the student to answer "what is number1 - number2 
    System.out.print("What is " + number1 + " * " + number2 + "?"); 
    int answer = input.nextInt(); 

    // grade the answer and display the result 
    if (number1 * number2 == answer) { 
     System.out.println("You are correct!"); 
     correctCount++; // increase the correct answer count 
    } else { 
     System.out.println("Your answer is worng. \n" + number1 + " * " 
       + number2 + " should be: " + (number1 * number2)); 
    } 

    count++; 

    System.out.println("\nWould you like more questions?"); 
    System.out.print(" Y or N ?"); 
    String option = input.nextLine(); 
} while (input.equals('y')); 
    System.out.println("\nYou've answered " + correctCount + " out of " 
     + count + " Correct"); 
} 

我知道我的代碼是有點亂,因爲我嘗試執行do-while循環,但沒能得到我的程序正常運行。

+0

您對當前代碼的確切問題是什麼? –

+0

'input.equals('y')'可能應該是類似'option.equalsIgnoreCase(「y」)''的東西。 –

+0

@MickMnemonic使用.equalsIgnoreCase(「y」)幫助,謝謝。 –

回答

2

您do-while循環的條件是while(input.equals('y')),但inputScanner對象,這並不在這個循環工作。既然你在用戶的選擇讀持續到option,它應該是while(option.equals("y")),或者更好的是,while(option.equalsIgnoreCase("y")),這將讓用戶輸入yY

2

您的主要問題是,您正在檢查input的值是否等於「y」,而不是因爲輸入是掃描器而不是字符串。

爲了使這項工作有一些你需要做的改變。

首先你要選擇do循環中的隨機數字,否則你會一遍又一遍地得到相同的數字。

其次,您要將option字符串的聲明移到循環外部,以便它具有可用的正確範圍。

因爲nextInt()方法只獲取int,所以剩下的行會留下,所以得到answer int後第三次需要調用input.nextLine()

第四條你想要使用.trim()當獲得下一行刪除空格和換行符時。

最後在檢查選項的值時使用.equalsIgnoreCase(),以便它可以用於大寫或小寫y。

 int correctCount = 0; // count the number correct question 
     int count = 0; // count the number of question 
     Scanner input = new Scanner(System.in); 



     String option; 

     do { 
      //2 random single-digit int 
      int number1 = (int)(Math.random() * 10); 
      int number2 = (int)(Math.random() * 10); 

      //prompt the student to answer "what is number1 - number2 
      System.out.print("What is " + number1 + " * " + number2 + "?"); 
      int answer = input.nextInt(); 
      input.nextLine(); 

      //grade the answer and display the result 
      if (number1 * number2 == answer) 
      { 
       System.out.println("You are correct!"); 
       correctCount++; //increase the correct answer count 

      } 
      else 
      { 
       System.out.println("Your answer is worng. \n" + 
      number1 + " * " + number2 + " should be: " + (number1 * number2)); 
      } 

      count++; 

      System.out.println("\nWould you like more questions?"); 
      System.out.print(" Y or N ?"); 

      option = input.nextLine().trim(); 
     } while (option.equalsIgnoreCase("Y")); 
+0

謝謝,一切都設法運作。將選項添加爲字符串確實有幫助。我本來應該這樣做。似乎我剛剛正確執行了正確的想法。 –

+0

是啊,你絕對是在正確的軌道上只是一些小事情 – SeanKelleyx

0

我不完全知道什麼是與掃描儀腳麻,但我更新了你的班級,現在工作正常。我在循環中移動了隨機數,這樣每次迭代的數字都會改變。我還將while條件改爲「input.next()。equalsIgnoreCase(」y「)」「。當它是input.nextLine()。equalsIgnoreCase(「y」)時,它停止一次迭代。我不熟悉掃描儀課程的獨特之處,並親自使用Reader。

public class Main { 
    public static void main(String[] args) { 
     int correctCount = 0; // count the number correct question 
     int count = 0; // count the number of question 
     Scanner input = new Scanner(System.in); 

     do { 
      //2 random single-digit int 
      int number1 = (int) (Math.random() * 10); 
      int number2 = (int) (Math.random() * 10); 

      //prompt the student to answer "what is number1 - number2 
      System.out.print("What is " + number1 + " * " + number2 + "?"); 
      int answer = input.nextInt(); 

      //grade the answer and display the result 
      if (number1 * number2 == answer) { 
       System.out.println("You are correct!"); 
       correctCount++; //increase the correct answer count 

      } else { 
       System.out.println("Your answer is worng. \n" + 
         number1 + " * " + number2 + " should be: " + (number1 * number2)); 
      } 

      count++; 

      System.out.println("\nWould you like more questions?"); 
      System.out.print(" Y or N ?"); 
     } while (input.next().equalsIgnoreCase("y")); 


     System.out.println("\nYou've answered " + correctCount + " out of " + count 
       + " Correct"); 
    } 
}