用我目前学到Java知识写一个简单的计算器,比较简陋
package base; import java.util.Scanner; public class Calc02 { public static void main(String[] args) { String str; double m; Scanner scanner = new Scanner(System.in); String c; do{ System.out.println("请输入进行的运算(+-*/)"); str=scanner.nextLine(); System.out.println("请输入运算的数字"); double x=scanner.nextDouble(); double y=scanner.nextDouble(); switch (str){ case "+": m=x+y; System.out.println(x+"+"+y+"="+m); break; case "-": m=x-y; System.out.println(x+"-"+y+"="+m); break; case "*": m=x*y; System.out.println(x+"*"+y+"="+m); break; case "/": if(y==0){ System.out.println("除零错误"); } else { m=x/y; System.out.println(x+"/"+y+"="+m); } break; } System.out.println("是否进行下次运算(Y/N)"); c = scanner.next(); scanner.nextLine(); }while (c.equals("Y")||c.equals("y")); scanner.close(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)