Java实现接受用户输入的两个数,分别求这两个数的和,差,积,商,模(条件语句)_编写一个java gui应用程序,从对话框中接受两个数字,然后计算这两个数的商_JadeXu07的博客-程序员宅基地

技术标签: JavaSE  java  switch  

import java.util.Scanner;

public class Main {
    
    /**
     * @author jadexu
     * @// TODO: 2020/12/17
     * 接受用户输入的两个数,分别求这两个数的和,差,积,商,模(带异常处理)
     */
    public static void main(String[] args) {
    
        //存储运算符
        final char[] ARITHMETIC = {
     '+', '-', '*', '/', '%' };
        //接收第一个数
        double num1 = getNum(1);
        //接收第二个数
        double num2 = getNum(2);
        //输出这两个数的和,差,积,商,模
        for (char a : ARITHMETIC) {
    
            System.out.printf("%.2f %c %.2f = %.2f\n",num1,a,num2,calculate(num1, num2, a));
        }
    }
    //接收数字(带异常处理,防止输入的数不是数字)
    private static double getNum(int n){
    
        Scanner sc = new Scanner(System.in);
        double num = 0;
        System.out.print("请输入第" + n + "个数:");
        try {
    
            num = sc.nextDouble();
        } catch (Exception e) {
    
            System.out.println("格式错误,重新输入");
            num = getNum(n);
        }
        return num;
    }
    //简易计算器
    public static double calculate(double num1, double num2, char a) {
    
        double result = 0;
        switch (a) {
    
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;
            case '%':
                result = num1 % num2;
                break;
        }
        return result;
    }
}

在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_44089205/article/details/111311478

智能推荐

计算机图形学之光线跟踪算法的研究与实现2017年我的优秀毕业论文-程序员宅基地

计算机图形学之光线跟踪算法的研究与实现2017年我的优秀毕业论文版权所有使用者请联系我 刘创QQ:9031885932.2.2 Phong光照模型事实上对于漫反射的物体表面,使用Lambert就足够,但是实际生活中并不存在这种理想的漫反射材质。Phong光照模型是现代真实图形学中提出的第一个有影响的光照明模型,不过该模型只考虑到了物体对直接光照的反射作用。此外,

ACM TJU 1556-程序员宅基地

Background Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single n...

vue npm 装包失败解决办法_while resolving: [email protected]_你要走就走的博客-程序员宅基地

在使用npm install问题npm ERR! code ERESOLVEnpm ERR! ERESOLVE unable to resolve dependency treenpm ERR!npm ERR! While resolving: [email protected] ERR! Found: [email protected] ERR! node_modules/webpacknpm ERR! peer webpack@"^4.0.0 || ^5.0.0" from html-we_while resolving: [email protected]

php 进一法格式化浮点数为特定位数 可用于格式化价格-程序员宅基地

我们经常用到进一法需要保留几位小数。这里提供一个函数。// $price为要格式化的数,$number为需要格式化的位数 使用此函数需要安装 BCMath 扩展function format_price($price,$number=2){ $format_price = bcadd($price,0,$number); $num = strripos($price,".");...

翁恺JAVA进阶期末测试_翁凯java语言期末考试题-程序员宅基地

编程题我自己 0 分,因为把输进来的宽度和高度搞反了!!!!!!!!!!!!!!!他先输进来的是宽度!!!!不想说了,都是泪。这是修改版的!!!!!选择题1 - 5 B C D D B6 - 10 A D C B A11 - 15 A A C D C16 - 20 C C A B C判断题21 - 25 X X √ √ ..._翁凯java语言期末考试题

[CareerCup] 8.2 Call Center 电话中心-程序员宅基地

8.2 Imagine you have a call center with three levels of employees: respondent, manager, and director. An incoming telephone call must be first allocated to a respondent who is free. If the responde..._candidate name: imagine you have a call center with three levels of employee

随便推点

微服务架构概述---扫盲片_颗粒度复杂度-程序员宅基地

不知从何而起,与微服务相关的技术已然成为检验一个程序员是否合格的第一要素,微服务这一名词无论是博客、社交媒体还是会议讲演 其热度绝对可以拔得头筹。可是当我试图百度出什么是微服务时,发现度娘的介绍含糊其辞,一个“新技术”也是足够让你微醺一会。图1、百度对微服务架构的解释然后试图搜些帖子看看,不过很少有人能讲清楚微服务到底是什么。于是便忍痛花了点银两买了这本名为《Spring Could ..._颗粒度复杂度

使用Dash开发交互式数据可视化网页--页面布局-程序员宅基地

Dash应用布局后续的操作前,需要安装如下Python包pip install dash==0.20.0 # The core dash backendpip install dash-renderer==0.11.2 # The dash front-endpip install dash-html-components==0.8.0 # HTML componen..._dash中的html.div()中的参数children

gdb在执行maintenance info program-spaces命令时coredump-程序员宅基地

coredump时的信息:(gdb) maintenance info program-spaces *** Error in `gdb': free(): invalid pointer: 0x0000000003c6bcf0 ***======= Backtrace: =========/lib64/libc.so.6(+0x7d1fd)[0x7fc875c0d1fd]gdb(fi

A JavaScript error occured in the main process-程序员宅基地

如图,打开某软件时,抛下错:A JavaScript error occured in the main processUncaught Exception:Error: Unable to find a valid app at Object.<anonymous> (E:\ProgramFiles(x86)\.....\resources\electron.asar\browser\init.js:121:9 at Object...

Idea常用快捷键_idea常用快捷代码-程序员宅基地

【前言】快捷键的使用是很重要的,高效率是从一点一滴体现出来的。【过程】一、调试快捷键1,rerun XXX,这个就是直接重新跑某个程序。2,这个相当于eclipse里面的f8,在idea里面是f9(运行到下一个断点处)直接跑完,到下一个断点停下,没有就直接跑完程序。3,停止项目或者程序。要是自己的main呢,点一下就停下了,要是Java web项目,则点2下,就把服务器给停..._idea常用快捷代码