Java教程

Java计算器

本文主要是介绍Java计算器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
/**
 * FileName: MethodDemo_01
 * Author:   86186
 * Date:     2021/11/2 21:32
 * Description: MethodDemo_01
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.itheima_01;
import java.util.Scanner;
/**
 * 〈一句话功能简述〉<br> 计算器
 * 〈MethodDemo_01〉
 *
 * @author 86186
 * @create 2021/11/2
 * @since 1.0.0
 */
public class MethodDemo_01 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while(true){
            int a = menu();
            if(a == 5){
                System.out.println("欢迎下次使用!!!!!!");
                break;
            }
            System.out.print("请输入两个操作数:");
            int num_1 = input.nextInt();
            int num_2 = input.nextInt();
            switch (a){
                case 1:
                    add(num_1,num_2);
                    break;
                case 2:
                    lose(num_1,num_2);
                    break;
                case 3:
                    ride(num_1,num_2);
                    break;
                case 4:
                    divide(num_1,num_2);
                    break;
            }
        }

    }
    //菜单方法
    public static int menu(){
        Scanner input = new Scanner(System.in);
        System.out.println("《-----计算器----》");
        System.out.println("功能如下:");
        System.out.println("1,加法");
        System.out.println("2,减法");
        System.out.println("3,乘法");
        System.out.println("4,除法");
        System.out.println("5,退出系统");
        System.out.print("请输入:");
        int choice = input.nextInt();
        return choice;
    }
    public static void add(int a,int b){
        System.out.println(" " + a + " + " + " " + b + " = " + " " + (a + b));
    }
    public static void lose(int a,int b){
        System.out.println(" " + a + " - " + " " + b + " = " + " " + (a - b));
    }
    public static void ride(int a,int b){
        System.out.println(" " + a + " * " + " " + b + " = " + " " + (a * b));
    }
    public static void divide(int a,int b){
        System.out.println(" " + a + " / " + " " + b + " = " + " " + (a / b));
    }


}

 

这篇关于Java计算器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!