当前位置: 首页 > news >正文

Day16编写一个计算机程序

package method;
import java.util.Scanner;
public class Demo6 {/**作业:*1  写四个方法,加减乘除*2  利用循环+switch进行用户交互*3  传递需要操作的两个数*4  输出结果*/public static void main(String[] args) {Scanner scanner = new Scanner(System.in);boolean score = true;while(score) {System.out.println("请输入第一个数");int numb1 = scanner.nextInt();System.out.println("请输入第二个数");int numb2 = scanner.nextInt();System.out.println("请输入预算符(+,-,*,/):");String operator = scanner.next();int result = 0;switch (operator) {case "+":result = add(numb1, numb2);break;case "-":result = sub(numb1, numb2);break;case "*":result = mul(numb1, numb2);break;case "/":if (numb2 != 0) {result = div(numb1, numb2);} else {System.out.println("除数不能为零");}break;default:System.out.println("非法运算符");break;}if(!operator.equals("非法运算符")&&!((operator.equals("/"))&&(numb2==0))){System.out.println("计算结果"+result);}System.out.print("是否继续?(yes  or  no):");String choice = scanner.next();if(!choice.equalsIgnoreCase("yes")){score = false;}}scanner.close();}public static int add( int a, int b) {return a+b;}public static int sub(int a,  int b) {return a-b;}public static int mul(int a, int b) {return a*b;}public static int div(int a, int b) {return a/b;}}
http://www.wxhsa.cn/company.asp?id=1333

相关文章:

  • 迷宫最短路径
  • 千靶日记-0003
  • COMSOL 6.3 下载+安装教程+激活教程:一站式下载安装激活操作说明
  • 20231427-田泽航-Linux命令实践
  • 202207_BUGKU_二维码GIF
  • 20250910NOIP模拟赛
  • 分治 NTT 一则
  • U604938 你不准卡 O(n sqrt n log L) 其中 L log L = sqrt n
  • 20250906
  • 【2025最新推荐】AI大模型API中转站 | 国内直连ChatGPT/Claude/Gemini全系API接口服务
  • 在用灵魂去感受另一个灵魂的震颤
  • html怎么写
  • 谁拿了谁的伞?
  • NSSCTF-misc
  • 百粉粉福
  • lc1024-视频拼接
  • 多元统计分析1
  • OI界的梗
  • 202404_QQ_ZIP嵌套
  • 无重复字符的最长子串-leetcode
  • 两个常见的 计数问题 trick
  • 搜维尔科技:Xsens人形机器人拟人动作AI训练,提升机器人工作精度与效率
  • 202110_绿盟杯_隐藏的数据
  • 【初赛】图 - Slayer
  • 线上课
  • 弹窗、抽屉、当前页和新开页,到底怎么选? - 智慧园区
  • POJ 2566 Bound Found
  • 搜维尔科技:Haption触觉力反馈系统,沉浸式远程呈现、数字孪生、混合现实和移动远程机器人
  • 飞书免费企业邮箱推荐
  • CF1140F Extending Set of Points