用Java编写一个最Low的计算器【初学者】
代码:
import java.util.Scanner;
/*
编写一个计算器程序
*/
public class OptSwitchDemo3 {
public static void main(String[] args) {
//创建对象
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个数据:");
int x = sc.nextInt();
System.out.print("请输入运算符:");
String str = sc.next();
System.out.print("请输入第二个数据:");
int y = sc.nextInt();
switch (str){
case "+":
System.out.println(x+str+y+"="+(x+y));break;
case "-":
System.out.println(x+str+y+"="+(x-y));break;
case "*":
System.out.println(x+str+y+"="+(x*y));break;
case "/":
System.out.println(x+str+y+"="+(x/y));break;
default:
System.out.println("输入错误!");
}
}
}
运行截图1:
运行截图2: