所以我完全是 Java 的菜鸟,并且正在尝试使用计算器。下面的代码是我到目前为止所拥有的。问题是代码在我添加标签“循环:”之前一直有效,在添加标签后,扫描仪由于某种原因出现错误。我想要循环标签,以便用户可以选择(代码底部的 if 语句)是否继续使用计算器。任何帮助,将不胜感激。
import java.util.Scanner;
public class MainClass {
public MainClass(){
loops:
Scanner input = new Scanner(System.in);
System.out.println("First Number: ");
int number1 = input.nextInt();
System.out.println("Second Number: ");
int number2 = input.nextInt();
System.out.println("Operator (+, -, /, *)");
String operation = input.next();
String cont = input.next();
int total;
if(operation.equals("+")){
total = number1 + number2;
System.out.println(total);
}
if(operation.equals("-")){
total = number1 - number2;
System.out.println(total);
}
if(operation.equals("*")){
total = number1 * number2;
System.out.println(total);
}
if(operation.equals("/")){
total = number1 / number2;
System.out.println(total);
}
System.out.println("Continue? Y/N: ");
if (cont.equalsIgnoreCase("n")){
break loops;
}
}
public static void main(String[] args) {
new MainClass();
}
}