我无法让这个程序正常工作,它需要 2 个数字然后做你告诉它做的数学类型。我写这篇文章只是为了刷新我几个月没有使用 java 的记忆。这是代码:
import java.util.Scanner;
public class math {
public static void main(String[] args){
Scanner one = new Scanner(System.in);
Scanner two = new Scanner(System.in);
System.out.print("Enter a number: ");
int n1 = one.nextInt();
System.out.print("Enter a second number: ");
int n2 = one.nextInt();
System.out.print("Add, subtract, multiply, or divide? ");
String f = two.nextLine();
System.out.println(f);
if (f=="add"){
System.out.println(n1 + n2);
}else if (f=="subtract") {
if (n1>n2){
System.out.println(n1 - n2);
}else{
System.out.println(n2 - n1);
}
}else if (f=="multiply"){
System.out.println(n1 * n2);
}else if (f=="divide"){
if (n1>n2){
System.out.println(n1 / n2);
}else{
System.out.println(n2 / n1);
}
}
}
}
编辑:谢谢大家,它适用于 f.equals()