package base.method; import java.util.Scanner; public class Demo6 { public static String equal; public static double rest; public static double a; public static double c; public static String b; public static void main(String[] args) { Demo6 calculator = new Demo6(); //创建对象调用函数 Scanner scanner = new Scanner(System.in); System.out.println("请输入第一个数字"); a = scanner.nextDouble(); System.out.println("请输入运算符"); b = scanner.next(); System.out.println("请输入第二个数字"); c = scanner.nextDouble(); //double rest = 0;//存储最后的计算结果 //if判断计算 /* if (b.equals("-")){ rest= calculator.sub(a,c); }else if (b.equals("+")){ rest= calculator.add(a,c); }else if (b.equals("*")){ rest= calculator.multiply(a,c); }else if (b.equals("/")){ rest= calculator.div(a,c); } System.out.println("结果为:"+rest); */ //do while判断 switch循环 do { switch (b) { case "+": rest = add(a, c); System.out.println("输入=获取结果:"); equal = scanner.next(); coninue(); break; case "-": rest = sub(a, c); System.out.println("输入=获取结果:"); equal = scanner.next(); coninue(); break; case "*": rest =multiply(a, c); System.out.println("输入=获取结果:"); equal = scanner.next(); coninue(); break; case "/": rest = div(a, c); System.out.println("输入=获取结果:"); equal = scanner.next(); coninue(); break; default: System.out.println("请输入正确的运算符!"); } }while (b.equals("+")||b.equals("-")||b.equals("*")||b.equals("/")); scanner.close(); } //加法方法 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 multiply(double a,double b){ return a*b; } //除法方法 public static double div(double a,double b){ return a/b; } //获得结果 或 再次输入 或重新计算 public static void coninue(){ Scanner scanner = new Scanner(System.in); if (equal.equals("=")){ System.out.println(a+b+c+"="+rest); System.out.println("输入运算符+,-,*,/继续输入c 重新开始计算 或 输入其他字符结束"); b=scanner.next(); if (b.equals("+")||b.equals("-")||b.equals("*")||b.equals("/")){ System.out.println("输入数字:"); c = scanner.nextDouble(); a=rest; } else if (b.equals("c")) { System.out.println("请输入第一个数字"); a = scanner.nextDouble(); System.out.println("请输入运算符"); b = scanner.next(); System.out.println("请输入第二个数字"); c = scanner.nextDouble(); } else { scanner.close(); System.out.println("结束运算!"); } }else { System.out.println("输入错误!"); } } }
标签:计算机程序,scanner,double,System,equals,简单,println,模拟,out From: https://www.cnblogs.com/zxpwys/p/16911210.html