내 프로그램은 파일에서 질문을 받고 대답을 인쇄합니다. 사용자는 대답을해야하고, 잘못 이해하면 프로그램은 포인트를 공제해야합니다. 각 질문은 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);
}
}
답변이 잘못 되었으면 요청을 계속하기 위해 while 회 돌이를 사용하는 것에 대해 생각해 보셨습니까? –
그래서 while 루프를 잘못 추가하면 내 if를 꺼내어 그 자리를 대신 할 것입니다 –