gpt4 book ai didi

Java 跳过第三个 case 语句

转载 作者:行者123 更新时间:2023-12-02 02:55:08 25 4
gpt4 key购买 nike

这是一个简单的计算器,我需要使用基本命令来运行它。这个项目的目标是对异常进行编程(非常简单),但是,我一生都无法弄清楚这一点。我到处都找过了。

无论是 if/else 语句还是 Switch/Case 语句,第三条语句总是会被跳过。当用户输入“m”时,应该将计算值保存到占位符变量中以便能够调用(同样, super 简单)。我在添加部分添加了一个默认的 case 语句,并将我的保存方法添加到了默认语句中,它工作得很好。其他所有命令(r 表示重新调用,c 表示清除,e 表示退出)也很好用。 m 用于保 stub 本不......

import java.util.Scanner;

public class Calculator {
public static Scanner input = new Scanner(System.in);
public static double placeHolder;

public static void clear() {
System.out.println("Save has been deleted, Screen has been cleared.");
}

public static void end() {
System.out.println("Program ended..");
input.close();
System.exit(0);
}

public static void save(double initValue) {
System.out.println("Number saved!");
placeHolder = initValue;
}

public static void recall() {
if (placeHolder != 0){
System.out.println("Memory Place Holder Set To: " + placeHolder);
}
else {
System.out.println("There is no data saved.");
}
}

public static void commands() {
System.out.println("e = end | c = clear | m = save | r = recall | o = continue");
String command = input.nextLine();
if (command.equals("e")){
end();
}
else if (command.equals("c")){
clear();
}
else if (command.equals("r")){
recall();
}

}




public static void main(String[] args) {
boolean loop = true;
while (loop == true){
commands();
System.out.println("Please enter what you would like to do: (+,-,*,/,%)");
String function = input.nextLine();
System.out.println("Enter the first number to be calucalted (If dividing, this is the numerator):");
double n1 = input.nextDouble();
System.out.println("Enter the second number to be calucalted (If dividing, this is the denominator):");
double n2 = input.nextDouble();

//=======================
// Addition
//=======================
if (function.equals("+")){
double sum = n1+n2;
System.out.println(n1+"+"+ n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
break;
case "c":
clear();
break;
case "m":
save(sum);
break;
case "r":
recall();
break;
default:
System.out.println("Default");
save(sum);
break;
}

}
//=======================
// Subtraction
//=======================
else if (function.equals("-")){
double sum = n1-n2;
System.out.println(n1 + "-" + n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
case "c":
clear();
case "m":
save(sum);
case "r":
recall();
}
}
//=======================
// Multiplication
//=======================
else if (function.equals("*")){
double sum = n1*n2;
System.out.println(n1 + "*" + n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
case "c":
clear();
case "m":
save(sum);
case "r":
recall();
}
}
//=======================
// Division
//=======================
else if (function.equals("/")){
double sum = n1/n2;
System.out.println(n1 + "/" + n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
case "c":
clear();
case "m":
save(sum);
case "r":
recall();
}
}
//=======================
// Mod
//=======================
else if (function.equals("%")){
double sum = n1%n2;
System.out.println(n1 + "%" + n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
input.nextLine();
switch (command){
case "e":
end();
case "c":
clear();
case "m":
save(sum);
case "r":
recall();
}
}
}

//=======================
// Dictate loop duration:
//=======================
System.out.println("Would you like to continue? (Y|N): ");
String ans = input.nextLine();
if (ans.equals("N") || ans.equals("n")){
System.out.println("Closing Program");
loop = false;
end();
}
}

}

有问题的主要代码是这样的:我知道其余的代码没有默认或中断语句。这个确实如此,我正在调试并试图找出 m 失败的原因。现在,如果你是他,它只会转到默认的 case 语句,这并不能解决问题。

 switch (command){
case "e":
end();
break;
case "c":
clear();
break;
case "m":
save(sum);
break;
case "r":
recall();
break;
default:
System.out.println("Default");
save(sum);
break;

================================================== =========================以下是针对事后查看这篇文章的任何人的修复:

public static void main(String[] args) {
boolean loop = true;
while (loop == true){
commands();
System.out.println("Please enter what you would like to do: (+,-,*,/,%)");
String function = input.nextLine();
System.out.println("Enter the first number to be calucalted (If dividing, this is the numerator):");
double n1 = input.nextDouble();
input.nextLine();
System.out.println("Enter the second number to be calucalted (If dividing, this is the denominator):");
double n2 = input.nextDouble();
input.nextLine();

//=======================
// Addition
//=======================
if (function.equals("+")){
double sum = n1+n2;
System.out.println(n1+"+"+ n2 +" = " + sum);
System.out.println("e = end | c = clear | m = save | r = recall");
String command = input.nextLine();
switch (command){
case "e":
end();
break;
case "c":
clear();
break;
case "m":
save(sum);
break;
case "r":
recall();
break;
}

}

最佳答案

  1. 在每次 input.nextDouble(); 之后,您需要调用 input.nextLine(); 来使用新行,请参阅 Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
  2. 删除每个 String command = input.nextLine(); 后不必要的 input.nextLine();

关于Java 跳过第三个 case 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com