2016-10-23 3 views
0

mein Programm soll Fragen von einer Datei nehmen und sie und die Antworten drucken. dann soll der Benutzer sie beantworten und wenn sie es falsch verstehen, soll das Programm Punkte abziehen. Jede Frage ist 5 wert und wenn du es falsch machst, verlierst du 2. Aber wenn du es falsch machst, sollte es dir immer wieder die Frage stellen und nicht nur zum nächsten gehen, wie kann ich das beheben? Und zeigen Sie dem Benutzer, wie viele Punkte er noch hat.Wie man Fragen von einer Datei loop

import java.util.Scanner; 

public class HW21 { 
    public static void main(String[] args) {  
    Scanner input = new Scanner(System.in); 
    HWData hd = new HWData(); 
    int currentScore = 5; 

int numberOfQuestions = hd.getNumberOfQuestions(); 
for (int questionIndex=0; questionIndex<numberOfQuestions; ++questionIndex) { 
    System.out.println(hd.getQuestion(questionIndex)); 
    System.out.println(); 
    System.out.println("1. " + hd.getAnswer1(questionIndex)); 
    System.out.println(); 
    System.out.println("2. " + hd.getAnswer2(questionIndex)); 
    System.out.println(); 
    System.out.println("3. " + hd.getAnswer3(questionIndex)); 
    System.out.println(); 
    System.out.println("4. " + hd.getAnswer4(questionIndex)); 

    int answer = input.nextInt(); 

    if (answer == hd.getCorrect(questionIndex)) { 
     System.out.println(" Great Job! You got the right answer!"); 
    } 
    else { 
     System.out.println(" You got the answer wrong."); 
     currentScore -= 2; 
    } 
} 

input.close(); 
System.out.println("Final score: " + currentScore); 

    } 

    } 
+0

Haben Sie darüber nachgedacht, eine while-Schleife zu verwenden, um weiterzufragen, ob die Antwort falsch ist? –

+0

also würde ich hinzufügen, in einer while-Schleife, wenn es falsch ist oder nehmen Sie meine wenn sonst und ersetzen Sie es mit der Weile –

Antwort

0

Eine andere Lösung könnte sein, die questionIndex durch ein zu reduzieren, so dass die Schleife an der gleichen Stelle bleibt, bis der Benutzer es richtig macht. etwas wie folgt aus:

import java.util.Scanner; 

public class HW21 { 
    public static void main(String[] args) {  
     Scanner input = new Scanner(System.in); 
     HWData hd = new HWData(); 
     int currentScore = 5; 

     int numberOfQuestions = hd.getNumberOfQuestions(); 
     for (int questionIndex=0; questionIndex<numberOfQuestions; ++questionIndex) { 
      System.out.println(hd.getQuestion(questionIndex)); 
      System.out.println(); 
      System.out.println("1. " + hd.getAnswer1(questionIndex)); 
      System.out.println(); 
      System.out.println("2. " + hd.getAnswer2(questionIndex)); 
      System.out.println(); 
      System.out.println("3. " + hd.getAnswer3(questionIndex)); 
      System.out.println(); 
      System.out.println("4. " + hd.getAnswer4(questionIndex)); 

      int answer = input.nextInt(); 

      if (answer == hd.getCorrect(questionIndex)) { 
       System.out.println(" Great Job! You got the right answer!"); 
      } 
      else { 
       System.out.println(" You got the answer wrong."); 
       qestionIndex--; 
       currentScore -= 2; 
      } 
     } 

     input.close(); 
     System.out.println("Final score: " + currentScore); 

    } 

} 
+0

Dieser Code ist schlechte Praxis, siehe http://stackoverflow.com/questions/30088401/change-index-inside- a-for-Schleife. – CyprUS

0

ich einige Pseudo-Code gebe für Sie durch zu arbeiten. Es klingt wie eine Hausaufgabe für mich, daher werde ich hier nicht den ganzen Code posten.

1. Get number of questions 2. Initialize totalScore to 5 // I am taking this from your code snippet 2. For all questions, repeat steps 3. Ask Question i 4. if answer(i) == correct totalScore = totalScore + 5 Move on to next question Else, totalScore = totalScore -2 Print totalScore Go back to Step 3 5. At this step, the loop is done. Print totalScore or whatever actions you need to do

Ja, der Go back Teil wird die Verwendung einiger flag Variable erfordern, die Antwort gibt an, ob richtig oder nicht.

Ich hoffe, dass Sie es jetzt ausarbeiten können. Übrigens scheint Ihr Code im Großen und Ganzen in Ordnung zu sein. Du bist auf dem richtigen Weg.