import java.util.Scanner; /** * @author 蓝色以太 * 通过键盘接收数值和字符,实现计算器功能。 */ public class Calculator { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入第一个数值:"); double num1=sc.nextDouble(); System.out.println("请输入运算字符(+-*/):"); String ch=sc.next(); System.out.println("请输入第二个数值:"); double num2=sc.nextDouble(); switch(ch){ case "+":{ System.out.println(num1+"+"+num2+"="+(num1+num2)); break; } case "-":{ System.out.println(num1+"-"+num2+"="+(num1-num2)); break; } case "*":{ System.out.println(num1+"*"+num2+"="+(num1*num2)); break; } case "/":{ System.out.println(num1+"/"+num2+"="+(num1/num2)); break; } default :{ System.out.println("输入错误!"); break; } } } }
时间: 2024-10-06 00:30:57