Java 计算器的加减乘除简单运算

package com.jian.method;
import java.util.Scanner; //不要忘了加上此行代码,否则不能定义scanner类
public class Demo06 {
    //定义scanner用来接收键盘输入的数据
    public static Scanner scanner=new Scanner(System.in);
    //加法运算
    public static void jia() {
        System.out.println("请输入第一个数");
        int x = scanner.nextInt();
        System.out.println("请输入第二个数");
        int y = scanner.nextInt();
        System.out.println(x+"+"+y+"的结果为"+(x+y));
    }
    //减法运算
    public static void jian() {
        System.out.println("请输入第一个数");
        int x = scanner.nextInt();
        System.out.println("请输入第二个数");
        int y = scanner.nextInt();
        System.out.println(x+"-"+y+"的结果为"+(x-y));
    }
    //乘法运算
    public static void cheng() {
        System.out.println("请输入第一个数");
        int x = scanner.nextInt();
        System.out.println("请输入第二个数");
        int y = scanner.nextInt();
        System.out.println(x+"*"+y+"的结果为"+(x*y));
    }
    //除法运算
    /*
    除法运算,被除数不能为零
     */
    public static void chu() {
        System.out.println("请输入第一个数");
        int x = scanner.nextInt();
        System.out.println("请输入第二个数");
        int y = scanner.nextInt();
        System.out.println(x+"/"+y+"的结果为"+(x/y));
        if(y==0){
            System.out.println("不能进行运算");
        }
        else{
            System.out.println(x+"/"+y+"的结果为"+(x/y));
        }
    }
//i,b都是创建的方法,然后调用i,b方法完成程序的选择与运算
public static void i() {

    System.out.println("********欢迎来到*********");
    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("*************************");
    System.out.println("请输入您要选择的算法:");
    int b = scanner.nextInt();
    //使用switch方法调用上面的运算
    switch (b) {
        case 1:
            jia();
            break;

        case 2:
            jian();
            break;

        case 3:
            chu();
            break;

        case 4:
            cheng();
            break;
    }
}
//在main方法中调用i方法
    public static void main(String[] args) {
        i();
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian绘梨衣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值