В школе мне пришлось сделать программу калькулятора. В программе мы спрашиваем пользователя, хотят ли они добавлять, вычитать, умножать или делить. В конце мы спрашиваем у пользователя, что они хотят продолжить программу или нет. Я еще не добавил часть цикла, но моя проблема заключается в том, что после отображения «Хочешь продолжить» программа просто выходит.Текст не печатается
package calculator;
import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
{
int o1; //first operand
int o2; //second operand
Scanner input = new Scanner (System.in);
System.out.println("Enter a choice:");
System.out.println("+ to add");
System.out.println("- to subtract");
System.out.println("* to multiply");
System.out.println("/ to divide");
System.out.println("X to exit");
String userChoice = input.nextLine();
if (userChoice.equals("X to exit")) {
System.exit(0);
}
System.out.println("Enter the first operand:");
o1 = input.nextInt();
System.out.println("Enter the second operand:");
o2 = input.nextInt();
if (userChoice.equals("+ to add")) {
System.out.println((o1) + (o2)); }
else if (userChoice.equals("- to subtract")) {
System.out.println((o1) - (o2)); }
else if (userChoice.equals("* to multiply")) {
System.out.println((o1) * (o2)); }
else if (userChoice.equals("/ to divide")) {
System.out.println((o1)/(o2)); }
System.out.println("Would you like to continue?");
System.out.println("Yes");
System.out.println("No");
String userPick = input.nextLine(); {
if (userPick.equals("Yes")) {
System.out.println("Ok."); }
else if (userPick.equals("No")) {
System.exit(0); }
}
}
}
// TODO Auto-generated method stub
}
Вам нужно какое-то время (правда) за пределами всего, чтобы все повторялось. –
У меня была проблема с _lot_, работая с 'Scanner' и 'nextLine' после' nextInt'. См. этот вопрос. http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – Pietu1998