java实现简易计算器功能
代码如下:
package method; import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String Next = "yes"; while (Next.equals("yes")){ System.out.println("请输入第一个数:"); double num1 =scanner.nextDouble(); System.out.println("请输入运算符:"); String var =scanner.next(); System.out.println("请输入第二个数:"); double num2 =scanner.nextDouble(); if (scanner.hasNextLine()){ switch (var){ case "+": System.out.println(num1 + "+" + num2 + "=" + add(num1,num2)); break; case "-": System.out.println(num1 + "-" + num2 + "=" + minus(num1,num2)); break; case "*": System.out.println(num1 + "*" + num2 + "=" + mul(num1,num2)); break; case "/": if (num2 != 0){ System.out.println(num1 + "/" + num2 + "=" + divide(num1,num2)); }else { System.out.println("除数不能为0"); } break; } } System.out.println("输入yes继续运算:"); Next = scanner.next(); } scanner.close(); } public static double add(double i,double j){ return (i + j); } public static double minus(double i,double j){ return (i - j); } public static double mul(double i,double j){ return (i * j); } public static double divide(double i,double j){ return (i / j); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)