2016-10-23 4 views
0

내 프로그램은 파일에서 질문을 받고 대답을 인쇄합니다. 사용자는 대답을해야하고, 잘못 이해하면 프로그램은 포인트를 공제해야합니다. 각 질문은 5의 가치가 있으며, 잘못 이해하게되면 2를 잃게됩니다. 하지만 문제가 계속 발생하면 다음 질문으로 계속 진행해야합니다. 어떻게 해결할 수 있습니까? 그리고 사용자에게 얼마나 많은 포인트를 남겼는지 보여줍니다.파일에서 질문을 반복하는 방법

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

답변이 잘못 되었으면 요청을 계속하기 위해 while 회 돌이를 사용하는 것에 대해 생각해 보셨습니까? –

+0

그래서 while 루프를 잘못 추가하면 내 if를 꺼내어 그 자리를 대신 할 것입니다 –

답변

0

또 다른 해결책은 사용자가 바로 그것을 얻을 때까지 루프가 같은 장소에 남아 있도록 하나를 사용하여 questionIndex을 줄일 수 있습니다. 이 같은 일 :

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

이 코드는 나쁜 습관입니다. http://stackoverflow.com/questions/30088401/change-index-inside- a-for-loop. – CyprUS

0

당신을 통해 작동하려면 내가 어떤 의사 코드를 제공하고 있습니다. 숙제를하는 것처럼 들리므로 여기에 전체 코드를 게시하지 마십시오.

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

은 예, Go back 부분은 대답이 올바른지 여부를 여부를 나타내는 일부 flag 변수의 사용을 필요로 할 것이다.

나는 당신이 지금 그것을 해결할 수 있기를 바랍니다. 그건 그렇고, 당신의 코드는 괜찮아 보인다. 당신은 올바른 길을 가고 있습니다.

관련 문제