如何循环文件中的问题

时间:2016-10-23 00:05:22

标签: java arrays loops input

我的程序应该从文件中提取问题并打印它们和答案。然后用户应该回答他们,如果他们弄错了,程序应该扣除积分。每个问题都值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);

  }

  }

2 个答案:

答案 0 :(得分: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);

    }

}

答案 1 :(得分: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变量来指示答案是否正确。

我希望你现在可以解决这个问题。顺便说一句,总的来说你的代码似乎没问题。你是在正确的道路上。