javaif语句随机输入两个数,实现简单加减运算
时间: 2023-05-13 11:05:55 浏览: 134
好的,以下是一个简单的 Java 代码实现:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数:");
int num1 = scanner.nextInt();
System.out.print("请输入第二个数:");
int num2 = scanner.nextInt();
System.out.print("请输入运算符(+或-):");
char operator = scanner.next().charAt(0);
int result;
if (operator == '+') {
result = num1 + num2;
} else if (operator == '-') {
result = num1 - num2;
} else {
System.out.println("非法运算符!");
return;
}
System.out.println("运算结果为:" + result);
}
}
希望能对您有所帮助!
阅读全文