Я новичок в программировании и хотел бы помочь.Как выполнить правильный цикл для викторины умножения
Я пытаюсь создать викторину умножения, которая бесконечно петляет, пока пользователь не вводит no. Кроме того, при представлении проблемы с умножением программа также должна отображать или не правильно вводить пользовательский ввод.
Когда пользователь не вводит никакой цикл, он остановится и отобразит, сколько правильных ответов из общего количества заданных вопросов.
любые идеи?
Вот мой код:
public static void main(String[] args) {
int correctCount = 0; // count the number correct question
int count = 0; // count the number of question
Scanner input = new Scanner(System.in);
// 2 random single-digit int
int number1 = (int) (Math.random() * 10);
int number2 = (int) (Math.random() * 10);
do {
// prompt the student to answer "what is number1 - number2
System.out.print("What is " + number1 + " * " + number2 + "?");
int answer = input.nextInt();
// grade the answer and display the result
if (number1 * number2 == answer) {
System.out.println("You are correct!");
correctCount++; // increase the correct answer count
} else {
System.out.println("Your answer is worng. \n" + number1 + " * "
+ number2 + " should be: " + (number1 * number2));
}
count++;
System.out.println("\nWould you like more questions?");
System.out.print(" Y or N ?");
String option = input.nextLine();
} while (input.equals('y'));
System.out.println("\nYou've answered " + correctCount + " out of "
+ count + " Correct");
}
Я знаю, что мой код немного беспорядка, потому что я пытался выполнить делать-то время цикла, но не был в состоянии получить мою программу, чтобы правильно работать.
Какая проблема возникает с текущим кодом? –
'input.equals ('y')' должно, вероятно, быть чем-то вроде 'option.equalsIgnoreCase (" y ")' вместо этого. –
@MickMnemonic using .equalsIgnoreCase ("y") помог, спасибо. –