你好,当我输入String" quit"时,我似乎无法使用while循环来终止代码。任何帮助将不胜感激。
package CalculatorProg;
import java.util.Scanner;
public class MainDriver {
public static void main(String[] args){
BinaryExpression operation = new BinaryExpression();
@SuppressWarnings("resource")
Scanner userinput = new Scanner(System.in);
while(true){
String calc;
String num1;
String num2;
num1 = userinput.next();
calc = userinput.next();
num2 = userinput.next();
String[] input = new String[5];
input[0] = num1;
input[1] = " ";
input[2] = calc;
input[3] = " ";
input[4] = num2;
if(calc.equals("+")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Add(x, y));
}
else if(calc.equals("-")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Subtract(x, y));
}
else if(calc.equals("*")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Multiply(x, y));
}
else if(calc.equals("/")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Divide(x, y));
}
else{
System.out.println("The operation is not valid.");
}
if(input[0].equalsIgnoreCase("quit")){
break;
}
}
System.exit(0);
}
}
答案 0 :(得分:1)
代码在calc = userinput.next();
上等待用户输入public boolean hasNext() 如果此扫描器的输入中有另一个标记,则返回true。在等待输入扫描时,此方法可能会阻塞。扫描仪不会超过任何输入。
答案 1 :(得分:0)
首先应该在自己的条件下验证输入。
示例:
package CalculatorProg;
import java.util.Scanner;
public class MainDriver {
public static void main(String[] args) {
BinaryExpression operation = new BinaryExpression();
@SuppressWarnings("resource")
Scanner userinput = new Scanner(System.in);
while (true) {
String calc;
String num1;
String num2;
num1 = userinput.next();
calc = userinput.next();
num2 = userinput.next();
String[] input = new String[5];
input[0] = num1;
input[1] = " ";
input[2] = calc;
input[3] = " ";
input[4] = num2;
if (!input[0].equalsIgnoreCase("quit")) {
if (calc.equals("+")) {
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Add(x, y));
} else if (calc.equals("-")) {
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Subtract(x, y));
} else if (calc.equals("*")) {
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Multiply(x, y));
} else if (calc.equals("/")) {
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Divide(x, y));
} else {
System.out.println("The operation is not valid.");
}
} else {
break;
}
}
System.exit(0);
}
}
答案 2 :(得分:0)
尝试检查'退出'在任何其他条件之前:
package CalculatorProg;
import java.util.Scanner;
public class MainDriver {
public static void main(String[] args){
BinaryExpression operation = new BinaryExpression();
@SuppressWarnings("resource")
Scanner userinput = new Scanner(System.in);
while(true){
String calc;
String num1;
String num2;
num1 = userinput.next();
if(num1.equalsIgnoreCase("quit")){
break;
}
calc = userinput.next();
num2 = userinput.next();
String[] input = new String[5];
input[0] = num1;
input[1] = " ";
input[2] = calc;
input[3] = " ";
input[4] = num2;
if(calc.equals("+")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Add(x, y));
}
else if(calc.equals("-")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Subtract(x, y));
}
else if(calc.equals("*")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Multiply(x, y));
}
else if(calc.equals("/")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Divide(x, y));
}
else{
System.out.println("The operation is not valid.");
}
}
System.exit(0);
}
}
问题可能在于它正试图解析“退出”问题。到整数,这会导致错误并且永远不会达到退出条件。
或者可能是代码正在等待输入&#39; calc = userinput.next();&#39;,但我不确定您是否尝试输入下两个输入< / p>
答案 3 :(得分:0)
代码可能会停留在第二个next()
来电。只需拨打一次nextLine()
电话,然后拨打String::split
String input = userinput.nextLine(); // Get an entire line
if (input.equalsIgnoreCase("quit")){ // Check the exit first
break;
}
// If it hasn't exited assume that its an equation
String[] inputSplit = input.split(); // Split the input by spaces
String calc inputSplit[1];
String num1 inputSplit[0];
String num2 inputSplit[2];
答案 4 :(得分:0)
我希望它适合你。在程序运行时,我会向用户提供一些帮助信息。你可以尝试一下! :d
package CalculatorProg;
import java.util.Scanner;
public class MainDriver {
public static void main(String[] args){
BinaryExpression operation = new BinaryExpression();
@SuppressWarnings("resource")
Scanner userinput = new Scanner(System.in);
while(true){
String calc;
String num1;
String num2;
System.out.print("Number 1 (if quit then program exits) =");
num1 = userinput.next();
if(num1.equalsIgnoreCase("quit")) {
System.out.println("program quit");
break;
}
System.out.print("Operator =");
calc = userinput.next();
System.out.print("Number 2 =");
num2 = userinput.next();
String[] input = new String[5];
input[0] = num1;
input[1] = " ";
input[2] = calc;
input[3] = " ";
input[4] = num2;
if(calc.equals("+")){
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + operation.Add(x,y));
//System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + (x+y));
}
else if(calc.equals("-")){
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + operation.Subtract(x, y));
//System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + (x-y));
}
else if(calc.equals("*")){
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + operation.Multiply(x, y));
//System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + (x*y));
}
else if(calc.equals("/")){
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + operation.Divide(x, y));
//System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + (x/y));
}
else{
System.out.println("The operation is not valid. You can give +, -, *, / for operator!");
}
}
System.exit(0);
}
}