我的程序不放弃当我问为什么?



我创建了一个4函数计算器。一切正常,除了当我让它退出时,它不会,它只是继续请求arg1和arg2。我设置了一个while循环来让它连续运行,但是当我输入&;quit&;时,它应该退出程序。我做错了什么?

import java.util.Scanner;
public class Calc{
public static void main(String[] args) { 
Scanner scan = new Scanner(System.in);
boolean done = true;
while (done == true) {
System.out.println("Enter operator: + - * / or quit: "); //gets operator
String operator = scan.next();
System.out.println("Enter arg1: "); //gets the first argument
double arg1 = scan.nextDouble();
System.out.println("Enter arg2: "); //gets the second argument
double arg2 = scan.nextDouble();
if (operator.equals("+")) {
add(arg1, arg2);
}
if (operator.equals("-")) {
minus(arg1, arg2);
}
if (operator.equals("*")) {
multiply(arg1, arg2);
}
if (operator.equals("/")) {
divide(arg1, arg2);
}
if (operator.equals("quit")) {
System.out.println("Thank you for using the CSC 220 calculator!");
done = false;
}
if((!operator.equals("+")) && (!operator.equals("-") && (!operator.equals("/")) && (!operator.equals("*")) && (!operator.equals("quit")))) {
System.out.println("Error. That is not a supported operator.");
}

}
}
static void divide (double arg1, double arg2){
System.out.println(arg1 + "/" + arg2 + " = " + (arg1 / arg2));

}
static void multiply (double arg1, double arg2){
System.out.println(arg1 + " * " + arg2 + " = " + (arg1 * arg2));

}
static void add (double arg1, double arg2){
System.out.println(arg1 + " + " + arg2 + " = " + (arg1 + arg2));

}
static void minus (double arg1, double arg2){
System.out.println(arg1 + " - " + arg2 + " = " + (arg1 - arg2));
}
}

请注意if语句的顺序。如果您不想要求用户输入arg1arg2,请确保在要求用户输入之前检查操作符是否为quit。正如在注释中提到的,将IDE的调试器与调试消息结合使用,可能有助于更好地理解为什么会发生某种行为。

在此之上,查看javadoc中的break;语句可能会有所帮助。

如果输入为quit,则应在接受操作符输入后中断循环。

import java.util.Scanner;
public class Calc{
public static void main(String[] args) { 
Scanner scan = new Scanner(System.in);
boolean done = true;
while (done == true) {
System.out.println("Enter operator: + - * / or quit: "); //gets operator
String operator = scan.next();
if(operator.equals("quit")) {
break;
}
System.out.println("Enter arg1: "); //gets the first argument
double arg1 = scan.nextDouble();
System.out.println("Enter arg2: "); //gets the second argument
double arg2 = scan.nextDouble();
if (operator.equals("+")) {
add(arg1, arg2);
}
if (operator.equals("-")) {
minus(arg1, arg2);
}
if (operator.equals("*")) {
multiply(arg1, arg2);
}
if (operator.equals("/")) {
divide(arg1, arg2);
}
if((!operator.equals("+")) && (!operator.equals("-") && (!operator.equals("/")) && (!operator.equals("*")) && (!operator.equals("quit")))) {
System.out.println("Error. That is not a supported operator.");
}

}
}
static void divide (double arg1, double arg2){
System.out.println(arg1 + "/" + arg2 + " = " + (arg1 / arg2));

}
static void multiply (double arg1, double arg2){
System.out.println(arg1 + " * " + arg2 + " = " + (arg1 * arg2));

}
static void add (double arg1, double arg2){
System.out.println(arg1 + " + " + arg2 + " = " + (arg1 + arg2));

}
static void minus (double arg1, double arg2){
System.out.println(arg1 + " - " + arg2 + " = " + (arg1 - arg2));
}
}

我同意,使用break是更好的方法。然而,我有点困惑为什么它不适合你,因为当你输入"退出",下面的代码退出,似乎与你提供的没有什么不同。

Scanner scan = new Scanner(System.in);
String operator;
boolean quit = false;

while (quit == false) {

operator = scan.next();
if (operator.equals("quit")) {quit = true;}
}

相关内容