简单计算器功能实现
- 实现人机交互
- 利用while和switch循环判断
- 写四个方法(加减乘除)
- 传入两个参数
代码
package com.huang.struct;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner way=new Scanner(System.in);
while(way.hasNextDouble())
{
double a=way.nextDouble();
System.out.println("第一个数为:"+a);
System.out.println("请输入符号");
String str=way.next();
System.out.println("请输入第二个数(输入非double数退出)");
double b=way.nextDouble();
switch (str) {
case "+":
System.out.println("结果为:" + add(a, b));
break;
case "-":
System.out.println("结果为" + minus(a, b));
break;
case "*":
System.out.println("结果为" + multiply(a, b));
break;
case "/":
System.out.println("结果为" + divide(a, b));
break;
default:
System.out.println("输入错误");
}
}
way.close();
}
public static double add(double a,double b){
return a+b;
}
public static double minus(double a,double b){
return a-b;
}
public static double multiply(double a,double b){
return a*b;
}
public static double divide(double a,double b){
return a/b;
}
}