2017-11-12 8 views
0

私のDrは、ユーザーが正しい答えを得ようとした回数を把握するように指示していました。ここにプログラムがあります。誰かが私にこれを手伝ってください。ループの試行回数を取得する

public static void main(String[] args) { 

    int a = (int) (Math.random() * 10); 
    int b = (int) (Math.random() * 10); 
    Scanner s = new Scanner(System.in); 
    System.out.println("What is: " + a + " + " + b + " ?"); 
    int answer = s.nextInt(); 
    while (answer != (a + b)) { ///tell him it's wrong 
     System.out.println("Incorrect. Try again."); 
     System.out.println("What is: " + a + " + " + b + " ?"); 
     answer = s.nextInt(); 
    } 
     System.out.println("Correct."); 
     /// how many tries did the user tried? H.W 
    } 
} 
+1

ループを使用した回数を数えます。 –

答えて

0

count変数を1つ持ち、whileループ内でそれを増やしてください。

public static void main(String[] args) { 

    int a = (int) (Math.random() * 10); 
    int b = (int) (Math.random() * 10); 
    Scanner s = new Scanner(System.in); 
    int count = 0; 
    System.out.println("What is: " + a + " + " + b + " ?"); 
    int answer = s.nextInt(); 
    while (answer != (a + b)) { ///tell him it's wrong 
     System.out.println("Incorrect. Try again."); 
     count++; //increase if the number of wrong answers 
     System.out.println("What is: " + a + " + " + b + " ?"); 
     answer = s.nextInt(); 
    } 
    System.out.println("Correct after " + count + " time(s)"); 
} 
+1

カウントを正確に使用した理由は何ですか?それは私には分かりません –

+0

count変数は外見の外で宣言され、ループ内で増加し、print文のループ外で使用されます – Apolozeus

0

最初は0に設定された変数がcountを言うと、ユーザが誤っているときは常にwhileループすなわち、各実行でそれをインクリメント取ることができます。ループの外側では、試行回数をcountとして印刷できます。コードで

あなたはこのようにそれを行うことができます..私は上記の答えのためのあなたのコメントを見

import java.util.*; 

class Main { 
    public static void main(String[] args) { 

    int a = (int) (Math.random() * 10); 
    int b = (int) (Math.random() * 10); 

    int count = 0; // setting variable to count number of tries 
    Scanner s = new Scanner(System.in); 
    System.out.println("What is: " + a + " + " + b + " ?"); 
    int answer = s.nextInt(); 

    while (answer != (a + b)) { ///tell him it's wrong 
     System.out.println("Incorrect. Try again."); 
     count++; // incrementing the number of tries i.e count 
     System.out.println("What is: " + a + " + " + b + " ?"); 
     answer = s.nextInt(); 
    } 

    System.out.println("Correct after " + count + " time(s)"); // displaying count 
    /// how many tries did the user tried? H.W 
    } 

} 
+0

わかります。おかげで多くの私の友だち –

0
public static void main(String[] args) { 

    int a = (int) (Math.random() * 10); 
    int b = (int) (Math.random() * 10); 
    Scanner s = new Scanner(System.in); 
    System.out.println("What is: " + a + " + " + b + " ?"); 
    int answer = s.nextInt(); 
    int count = 0; 
    while (answer != (a + b)) { ///tell him it's wrong 
     count++; // Why count ?? 
     System.out.println("Incorrect. Try again."); 
     System.out.println("What is: " + a + " + " + b + " ?"); 
     answer = s.nextInt(); 
    } 
     System.out.println("Correct. It took you " + count + "attempt/s."); 
     /// how many tries did the user tried? H.W 

    } 
} 

。答えは100%正しいです。

countのご利用に関するご質問があります。

どのように手動で行うか教えてください。あなたがあなたの友人に質問をして、4回試みた後、最後の試行、すなわち5回目の試行で正しい答えを得た場合、どのように "ああ、5回試してみた! ??

あなたは正しい答えになるまで、あなたの指先でそれを数えますか?

あなたがコードで行うことは、変数をzero(0)に開始/初期化し、ユーザーからの入力を受け取ると(つまり回答)、正しいかどうかをチェックします。正しい場合は...カウント値を1増加させます(最初の指を開き、 "試してみました1回")。間違っている場合は(指を開いて「いいえ、もう一度やり直してください。 1、そして間違った試行でカウンタを何度も増やし続ける。そして答えが一致すれば、カウンターを上げて "Hurray ..あなたは正しい答えを得て、count回かかりました。"

あなたの質問を解決したいと思います。 !!

+0

あなたは素晴らしいサポート<3 –

関連する問題