java基础使用scanner类实现简单二位运算

本文章为个人笔记积累

import java.util.Scanner;

public class EzCalculator {

    public static void main(String[] args) {

        while (true) {
            System.out.println("输入数字选择使用的运算方法");
            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);
            int x = scanner.nextInt();
            switch (x) {
                case 1:
                    System.out.println("你选择的是加法");
                    System.out.println("请输入第一个数:");
                    addition();
                    break;
                case 2:
                    System.out.println("你选择的是减法");
                    System.out.println("请输入第一个数:");
                    subtraction();
                    break;
                case 3:
                    System.out.println("你选择的是乘法");
                    System.out.println("请输入第一个数:");
                    multiplication();
                    break;
                case 4:
                    System.out.println("你选择的是除法");
                    System.out.println("请输入第一个数:");
                    division();
                    break;
                case 5:
                    System.out.println("你选择的是取余数");
                    System.out.println("请输入第一个数:");
                    remainder();
                    break;
                default:
                    System.out.println("无效输入");
                    break;
            }
            System.out.println("输入”1“继续运算,输入”2“结束");
            int i = scanner.nextInt();
            if (i == 1) {
                System.out.println("继续");
                continue;
            }
            if (i == 2) {
                System.out.println("结束");
                break;
            }
        }
    }

    public static void addition(){
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        System.out.println("请输入第二个数");
        int y = scanner.nextInt();
        System.out.println("结果为:"+(x+y));
    }

    public static void subtraction(){
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        System.out.println("请输入第二个数");
        int y = scanner.nextInt();
        System.out.println("结果为:"+(x-y));
    }
    public static void multiplication(){
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        System.out.println("请输入第二个数");
        int y = scanner.nextInt();
        System.out.println("结果为:"+(x*y));
    }
    public static void division(){
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        System.out.println("请输入第二个数");
        int y = scanner.nextInt();
        System.out.println("结果为:"+(x/y));
    }
    public static void remainder(){
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        System.out.println("请输入第二个数");
        int y = scanner.nextInt();
        System.out.println("结果为:"+(x%y));
    }


}

你可能感兴趣的:(java,开发语言,后端)