简陋计算器加减乘除

简陋计算器加减乘除运算

废话不多说 直接撸代码

package com.niu.www.method;

import java.util.Scanner;

public class Demo04 {
    static int  selection;
    static int a;
    static int b;
  
    public static void main(String[] args) {
        boolean flag = true;
        while (flag){
            System.out.println("选择1进行加法");
            System.out.println("选择2进行减法");
            System.out.println("选择3进行乘法");
            System.out.println("选择4进行除法");
            System.out.println("选择5退出系统");
            Scanner scanner = new Scanner(System.in);

            while (scanner.hasNextInt()){
               selection = scanner.nextInt();
                if (selection!=1&&selection!=2&&selection!=3&&selection!=4&&selection!=5){
                    System.out.println("输入有误请重新输入");
                    continue;
                }else {
                    break;
                }

            }
            switch (selection){
                case 1:{
                    System.out.println("请输入第一个数字a");

                  if (scanner.hasNextInt()){
                       a= scanner.nextInt();
                    }
                    System.out.println("请输入第二个数字b");
                  if (scanner.hasNextInt()){
                      b= scanner.nextInt();
                  }

                    System.out.println(a+b);
                  break;
                }

                case 2:{
                    System.out.println("请输入第一个数字a");

                    if (scanner.hasNextInt()){
                        a= scanner.nextInt();
                    }
                    System.out.println("请输入第二个数字b");
                    if (scanner.hasNextInt()){
                        b= scanner.nextInt();
                    }

                    System.out.println(a-b);
                    break;
                }
                case 3:{
                    System.out.println("请输入第一个数字a");

                    if (scanner.hasNextInt()){
                        a= scanner.nextInt();
                    }
                    System.out.println("请输入第二个数字b");
                    if (scanner.hasNextInt()){
                        b= scanner.nextInt();
                    }

                    System.out.println(a*b);
                    break;
                }
                case 4:{
                    System.out.println("请输入第一个数字a");

                    if (scanner.hasNextInt()){
                        a= scanner.nextInt();
                    }
                    System.out.println("请输入第二个数字b");
                    if (scanner.hasNextInt()){
                        b= scanner.nextInt();
                    }

                    System.out.println(a/b);
                    break;
                }

                case 5:
                    flag=false;
                    break;
            }


            }
        }
    }


posted @ 2020-05-13 13:52  步步登高  阅读(152)  评论(0)    收藏  举报