2016-06-22 15 views
-4

私は、ユーザーに質問して合計を求めるJavaプログラムを持っています。私は、プログラムの最後に答えられた質問の正しい合計を表示しようとしています。しかし、私はどのようにするか分からない、いくつか私を助けてください! 私のコードがlのように見える例です:しかし、これは私の実際のコードではありません、これは別のソースからです!Java!ユーザーが質問に答えるのを終えた後、正解と不正解の質問を表示するには

Import java.util.Scanner; 


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

     // The array of questions. 
     String questions[] = { 
       "Plants derive most of their dry mass from the air.", 
       "Aluminium is the most common metal in the Earth's crust.", 
       "Vitamin C has be shown to prevent colds.", 
       "We lose the most heat through our heads.", 
       "Dogs are unable to digest chocolate.", 
       "Apple pips contain cyanide.", 
       "Cholesterol is a nat 
       "When you're on a diet, you lose weight by oxidising fat to a gas and exhaling it.", 
       "Human beings are unable to sense when the oxygen level of the air is low.", 
       "Most of the Y chromosome is passed unchanged from father to son" }; 

     // The array of answers. The entries correspond to the questions. 
     boolean answers[] = { true, true, false, false, false, true, false, 
           true, true, true }; 

     // Display the opening text. 
     System.out.println("Answer each of the following questions with 't' (true) or 'f' (false)"); 

     // We'll use this to get user input. 
     Scanner input = new Scanner(System.in); 

     // Add up the user's score here as we go along. 
     int score = 0; 

     // The is the index into the questions array and the answers array. 
     int questionNumber = 0; 

     // Create a blank line. 
     System.out.println(); 

     // The do-while loop will keep running while questionNumber is less 
     // than the question array length. 
     do { 
      // Display the question 
      System.out.println(questions[questionNumber]); 

      // Display a little prompt to make it clearer that the user has to 
      // enter something. 
      System.out.print("> "); 

      // Get the user's answer. 
      String userAnswer = input.nextLine(); 

      // Check that the user has entered t or f. 
      if (!userAnswer.equals("t") && !userAnswer.equals("f")) { 
       System.out.println("Please enter t for true or f for false.\n"); 

       // Invalid input! 
       // Skip the rest of this loop iteration and ask the same question again. 
       continue; 
      } 

      // Check the answer. 
      if (userAnswer.equals("t") && answers[questionNumber] == true) { 
       // If the answer's t and the right answer is "true", the answer was correct. 
       score++; 
       System.out.println("correct\n"); 
      } else if (userAnswer.equals("f") && answers[questionNumber] == false) { 
       // If the answer's f and the correct answer is "false", the answer was correct. 
       System.out.println("correct\n"); 
       score++; 
      } 
      else { 
       // Wrong answer! 
       System.out.println("incorrect!\n"); 
      } 

      // Now we can move to the next question when we go round the loop again. 
      questionNumber++; 

     } while (questionNumber < questions.length); // end of do-while. 

     // This isn't really necessary, but closing the Scanner prevents a warning icon in Eclipse. 
     input.close(); 

     // Tell the user their score. 
     System.out.println("You scored: " + score); 

     // Rank the score! Only one of the alternatives below will execute. 
     // Java will check them in order from top to bottom. 
     if(score < 5) { 
      // Less than 5 -- not so good. 
      System.out.println("Hmmm, maybe you're the artistic type. Try the test again!"); 
     } 
     else if(score < 8) { 
      // The score wasn't less than 5, but it IS less than 8. 
      System.out.println("Not bad! But have another go."); 
     } 
     else if(score <= 9) { 
      // The score wasn't less than 8, but it IS less than, or equal to, 9. 
      System.out.println("Pretty good! But no perfect. Try again!"); 
     } 
     else { 
      // The score was more than 9 -- must be 10 because we've only got 10 questions. 
      System.out.println("You're a certified science genius!"); 
     } 

    } 
} 
+0

:あなたはこのような出力を行うことができます)DO-whileループの外challenge-science-quiz.html)(目に見えるwhe n "Click for example solution"ボタンをクリックします)。あなたのコードではありません。 – RealSkeptic

+0

ええ、それはまさにそれのような何かに取り組んでいます!だから私は無料のソースコードとしてそれを見つけました。なぜそれを例として掲示しないのかを理解して、何を言いたいのですか? –

+0

ここに私の電話を使って、私のコードは私のシステムにあります。私はそれがあなたの問題ではないことを願っています! –

答えて

0

ユーザーが質問に正しく答えたときに、リストを使用して質問番号を追加することができます。 あなたが

ArrayList<Integer> correctQuestions = new ArrayList<Integer>(); 

を追加する必要があり、あなたがdo-whileループは、その後、あなたがdo-whileループでは、あなたは(答えはあなたのプログラムの終了時

correctQuestions.add(questionNumber); 

正しいときに追加する必要があります前に、 https://www.caveofprogramming.com/java-exercises/java-exercise-(このコードは、[プログラミングの洞窟]から来た

System.out.println("You answered the following questions correctly"); 
for (int k : correctQuestions) { 
    System.out.println(questions[k]); 
} 
+0

うわー、このrlly助けてくれてありがとう!もう一度ありがとう –

+0

@Binyamin Bgod答えとして私の記事を記入することは自由である;) – Blobonat

0

は、単にあなたのクラスでのユーザーの回答を含むブール配列を宣言:

あなたの中に今
public boolean[] userAnswers = new boolean[questions.length]; //remove the access modifier if you want to declare the array inside main instead of the class 

DO-whileループは、ユーザの入力を解析した後、これを実行します。

userAnswers[questionNumber] = userAnswer.equals("t"); 

をdo-whileループの外で(Scannerを閉じた後)、次の手順を実行してすべての質問とユーザーの回答を印刷します。

for (int i = 0; i < questions.length; i++) { 
    System.out.println("User answered \"" + userAnswers[i] + "\" on question " + (i + 1) + ": " + questions[i]); 
} 
関連する問題