简易计算器(求大佬能指导完善)
-
要求:
- 键盘录入,实现加减乘除功能
- 能循环接收新的数据,通过用户交互实现
-
思路:
- 写加减乘除四个方法
- 利用循环+switch进行用户交互
- 传递需要操作的两个数
- 输出结果
-
代码(未达成循环交互):
package com.Zzack.base; import java.util.Scanner; public class Demo09 { //键盘录入计算器 public static void main(String[] args) { Scanner s = new Scanner(System.in); double x = s.nextDouble() ; String y = s.next(); double z = s.nextDouble(); double n1 = jia(x,z); double n2 = jian(x,z); double n3 = cheng(x,z); double n4 = chu(x,z); String f = y; switch (f){ case "+" : System.out.println(n1); break; case "-" : System.out.println(n2); break; case "*" : System.out.println(n3); break; case "/" : System.out.println(n4); break; } s.close(); } public static double jia(double a,double b){ return a+b; } public static double jian(double a,double b){ return a-b; } public static double cheng(double a,double b){ return a*b; } public static double chu(double a,double b){ return a/b; } }