用scanner和switch编写一个控制台计算器
package com.lemon.method;
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的第1个数据:");
//从键盘获取第一个数据
float num1= scanner.nextFloat();
System.out.println("请输入您的运算字符 + - * / ");
String str=scanner.next();
//从键盘获取第二个数据
System.out.println("请输入您的第2个数据:");
float num2= scanner.nextFloat();
scanner.close();
f(num1,str,num2);
}
public static void f(float a,String c,float b){
switch (c){
case "+":
System.out.println("计算结果为:"+a+"+"+b+"="+(a+b));
break;
case "-":
System.out.println("计算结果为:"+a+"-"+b+"="+(a-b));
break;
case "*":
System.out.println("计算结果为:"+a+"*"+b+"="+(a*b));
break;
case "/":
System.out.println("计算结果为:"+a+"/"+b+"="+(a/b));
break;
default:
System.out.println("输入有误!");
}
}
}
//运行结果