我是靠谱客的博主 飘逸绿草,这篇文章主要介绍java使用循环switch语句编写一个简易的计算器,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.LoganCai.struct; import java.util.Scanner; //使用循环switch语句编写一个简易计算器 public class Calculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char s; do { System.out.println("请输入两个数字进行简单数学运算,加减乘除以及求余,如:12 * 34"); double a = scanner.nextDouble();//输入第一个数字 char x = scanner.next().charAt(0);//输入字符 + - * / % double b = scanner.nextDouble();//输入第二个数字 double result;//存储运算结果 switch (x) { case '+': result = a + b; System.out.println("加法运算结果为:" + result); break; case '-': result = a - b; System.out.println("减法运算结果为:" + result); break; case '*': result = a * b; System.out.println("乘法运算结果为:" + result); break; case '/': if (b == 0) { System.out.println("运算错误,除数不能为0!"); } else { result = a / b; System.out.println("除法运算结果为:" + result); } break; case '%': if (b == 0) { System.out.println("运算错误,除数不能为0!"); } else { result = a % b; System.out.println("求余运算结果为:" + result); } break; default: System.out.println("您输入的算式格式有误"); } System.out.println("是否重新开始进行运算?请输入y/n"); s = scanner.next().charAt(0); }while (s =='y'); System.out.println("感谢您的使用!"); scanner.close(); } }

程序执行结果:
在这里插入图片描述

最后

以上就是飘逸绿草最近收集整理的关于java使用循环switch语句编写一个简易的计算器的全部内容,更多相关java使用循环switch语句编写一个简易内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部