Java 如果语句未通过,则启用Else
因此,我是一名noob编码员(网站新手),试图自学,目前我正在制作一个简单的计算器。在Atm中,我用一个检查输入的if语句向它添加了pi。然而,当我输入一个double时,它没有任何作用,我也不知道为什么Java 如果语句未通过,则启用Else,java,if-statement,calculator,Java,If Statement,Calculator,因此,我是一名noob编码员(网站新手),试图自学,目前我正在制作一个简单的计算器。在Atm中,我用一个检查输入的if语句向它添加了pi。然而,当我输入一个double时,它没有任何作用,我也不知道为什么 package testing; import java.util.Scanner; public class Calculator { @SuppressWarnings("resource") public static void main(String[] args) {
package testing;
import java.util.Scanner;
public class Calculator {
@SuppressWarnings("resource")
public static void main(String[] args) {
String piCheck = "pi";
Double n1 = null;
Scanner reader = new Scanner(System.in);
System.out.println("Welcome to Calculator!");
System.out.println(" ");
System.out.println("Enter a number, ");
System.out.println("or enter 'pi' to insert PI,");
if (reader.nextLine().toLowerCase().contains(piCheck)) {
n1 = Math.PI;
} else {
n1 = reader.nextDouble();
}
Scanner reader2 = new Scanner(System.in);
System.out.println("Would you like to multiply, divide, add, subtract?");
String uOperation = reader2.nextLine();
int operation = 1;
switch (uOperation.toLowerCase()) {
case "multiply":
operation = 1;
break;
case "multiplication":
operation = 1;
break;
case "times":
operation = 1;
break;
case "divide":
operation = 2;
break;
case "division":
operation = 2;
break;
case "add":
operation = 3;
break;
case "addition":
operation = 3;
break;
case "subtract":
operation = 4;
break;
case "subtraction":
operation = 4;
break;
default:
System.out.println("Invalid operation! Rerun Calculator!");
break;
}
if (operation == 1) {
System.out.println("What do you want to multiply " + n1 + " by?");
} else if (operation == 2) {
System.out.println("What do you want to divide " + n1 + " by?");
} else if (operation == 3) {
System.out.println("What do you want to add to " + n1 + "?");
} else if (operation == 4) {
System.out.println("What do you want to subtract from " + n1 + "?");
} else {
System.out.println("Something went wrong!");
}
double n2 = reader.nextDouble();
System.out.println("Here's your result!");
double resultM = n1*n2;
double resultD = n1/n2;
double resultA = n1+n2;
double resultS = n1-n2;
if (operation == 1) {
System.out.println(n1 + " multiplied by " + n2 + " equals:");
System.out.println(resultM);
} else if (operation == 2) {
System.out.println(n1 + " divided by " + n2 + " equals:");
System.out.println(resultD);
} else if (operation == 3) {
System.out.println(n1 + " plus " + n2 + " equals:");
System.out.println(resultA);
} else if (operation == 4) {
System.out.println(n1 + " take away " + n2 + " equals:");
System.out.println(resultS);
} else {
System.out.println("Something went wrong!");
}
}
}您的问题是您正在读取条件中的输入,因此如果您输入一个double,else子句希望输入更多的输入,因为第一个输入已经被
reader.nextLine()使用。
您必须保存输入并重新使用它:
String input = reader.nextLine();
if (input.toLowerCase().contains(piCheck)) {
n1 = Math.PI;
} else {
n1 = Double.parseDouble(input);
}
您的问题是,您正在读取条件中的输入,因此如果您输入一个double,else子句希望输入更多的输入,因为第一个输入已经被
reader.nextLine()
使用
您必须保存输入并重新使用它:
String input = reader.nextLine();
if (input.toLowerCase().contains(piCheck)) {
n1 = Math.PI;
} else {
n1 = Double.parseDouble(input);
}
您已经在通过执行
reader.nextLine()读取输入代码>。您只需将其解析为double,而不是执行n1=reader.nextDouble()代码>。欢迎使用stackoverflow!看起来你可以从你的问题中删减很多,这个问题仍然可以重现。这有助于更快地理解您的问题。请阅读。另外:你的缩进看起来有点不对劲,请修复这些东西,这样你就不会冒问题被否决的风险了!您已经在通过执行reader.nextLine()读取输入代码>。您只需将其解析为double,而不是执行n1=reader.nextDouble()代码>。欢迎使用stackoverflow!看起来你可以从你的问题中删减很多,这个问题仍然可以重现。这有助于更快地理解您的问题。请阅读。另外:你的缩进看起来有点不对劲,请修复这些东西,这样你就不会冒问题被否决的风险了!