Я хочу, чтобы он снова зациклился, когда пользователь вводит «Y» или «y» и уходит, когда они вводят «N» или «n». Опция quitting работает, однако, когда они вводят Y/y, она показывает первую систему, но не позволяет пользователю выбирать, какую операцию они хотят делать. Вместо этого опция продолжения всплывает снова и запрещает пользователю делать какой-либо выбор.Do While Loop Пропуск пользовательского ввода
Вот код:
import java.util.Scanner;
public class Calc2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double numOne, numTwo, ans;
String option;
do {
System.out.println(
"For addition press '1', for subtraction press '2', for division press '3', for multiplication press '4'");
String choice = input.nextLine();
if (choice.contains("1")) {
System.out.println("Enter the first number : ");
numOne = input.nextDouble();
System.out.println("Enter the second number : ");
numTwo = input.nextDouble();
ans = numOne + numTwo;
System.out.println("The answer is: " + ans + " ya bish.");
}
else if (choice.contains("2")) {
System.out.println("Enter the first number : ");
numOne = input.nextDouble();
System.out.println("Enter the second number : ");
numTwo = input.nextDouble();
ans = numOne - numTwo;
System.out.println("The answer is: " + ans + " ya bish.");
} else if (choice.contains("4")) {
System.out.println("Enter the first number : ");
numOne = input.nextDouble();
System.out.println("Enter the second number : ");
numTwo = input.nextDouble();
ans = numOne * numTwo;
System.out.println("The answer is: " + ans + " ya bish.");
} else if (choice.contains("3")) {
System.out.println("Enter the first number : ");
numOne = input.nextDouble();
System.out.println("Enter the second number : ");
numTwo = input.nextDouble();
ans = numOne/numTwo;
System.out.println("The answer is: " + ans + " ya bish.");
}
System.out.println("Press 'Y' to continue or 'N' to quit.");
option = input.next();
} while (option.equals("Y") || option.equals("y"));
if (option.equals("N") || option.equals("n")) {
System.out.println("Thank you. ");
}
}
}
Если кто-нибудь может мне помочь, было бы весьма признателен. Благодаря!
Рекомендуемая литература: [** Как отлаживать небольшие программы **] (http://ericlippert.com/ 2014/03/05/how-to-debug-small-programs /) – durron597
Возможная дубликация [Пропуск nextLine() после использования следующих(), nextInt() или других методов nextFoo()] (http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-next-nextint-or-other-nextfoo-methods) – TNT