java写一个简单的计算器 加减乘除1
作者:互联网
public class HelloWord {
public static void main(String[] args){
//用scanner接收输入信息
Scanner scanner = new Scanner(System.in);
//接收多个信息 每一个以回车结束
System.out.println("输入第一个参数a");
double a = scanner.nextDouble();
System.out.println("输入运算符");
String oper = scanner.next();
System.out.println("输入第二个参数b");
double b = scanner.nextDouble();
//声明类对象 用来调用方法
HelloWord helloWord = new HelloWord();
//通过运算符判断加减乘除
switch(oper){
case "+":
System.out.println("加法"+helloWord.add(a,b));
//不要忘了break 否则顺序执行
break;
case "-":
System.out.println("减法"+helloWord.sub(a,b));
break;
case "*":
System.out.println("乘法"+helloWord.mult(a,b));
break;
case "/":
System.out.println("除法"+helloWord.div(a,b));
break;
}
}
//声明加减乘除的方法
public static double add(double a,double b){
return a + b;
}
public static double sub(double a,double b){
return a - b;
}
public static double mult(double a,double b){
return a * b;
}
public static double div(double a,double b){
return a/b;
}
}
标签:java,scanner,double,System,计算器,println,out,public,加减乘除 来源: https://www.cnblogs.com/weijie1215/p/16331130.html