<>B站狂神计算器作业

我写的计算器
package com.chen.method; import java.util.Scanner; //简易计算器 public class Demo7 {
public static void main(String[] args) { //输入操作数1 Scanner scanner1 = new Scanner
(System.in); System.out.println("请输入操作数1:"); float number1 = scanner1.nextFloat(
); //输入操作数2 Scanner scanner2 = new Scanner(System.in); System.out.println(
"请输入操作数2:"); float number2 = scanner2.nextFloat(); //输入操作 Scanner scanner = new
Scanner(System.in); System.out.println("请输入操作(+,-,*,/):"); String symbol =
scanner.next(); //调用方法 switch (symbol){ case "+": float sum = add(number1,
number2); System.out.println(sum); break; case "-": float difference =
subtraction(number1,number2); System.out.println(difference); break; case "*":
float product = multiplication(number1,number2); System.out.println(product);
break; case "/": float quotient = division(number1,number2); System.out.println(
quotient); break; } scanner1.close(); scanner2.close(); scanner.close(); } //加法
public static float add(float a,float b){ float sum = a + b; return sum; } //减法
public static float subtraction(float c,float d){ float difference = c - d;
return difference; } //乘法 public static float multiplication(float e,float f){
float product = e * f; return product; } //除法 public static float division(float
g,float h){ float quotient = g / h; return quotient; } }
网上搜的进阶版本
package com.chen.method; import java.util.Scanner; //计算器优化 public class Demo8 {
public static void main(String[] args) { System.out.println("请输入第一个数:"); Scanner
scanner= new Scanner(System.in); float a = scanner.nextFloat(); System.out.
println("请输入运算符(+,-,*,/):"); Scanner scanner1 = new Scanner(System.in); String b
= scanner1.next(); System.out.println("请输入下一个数:"); Scanner scanner2 = new
Scanner(System.in); float c = scanner2.nextFloat(); while (true) { //点睛之笔! float
count= count(a,b,c); a = count; System.out.println("继续输入运算符进行运算,或输入Q退出程序...");
b= scanner2.next(); if (b.equals("Q")||b.equals("q")) { break; } System.out.
println("请输入数据..."); c = scanner1.nextFloat(); } } public static float count(
float a,String b,float c){ switch (b) { case "+": System.out.println("当前运算结果为"+
add(a, c)); return add(a, c); case "-": System.out.println("当前运算结果为"+sub(a, c));
return sub(a, c); case "*": System.out.println("当前运算结果为"+mul(a, c)); return mul(
a, c); case "/": System.out.println("当前运算结果为"+div(a, c)); return div(a, c);
default: System.out.println("非法输入"); return 0; } }; public static float add(
float a, float b) { return a + b; } public static float sub(float a, float b) {
return a - b; } public static float mul(float a, float b) { return a * b; }
public static float div(float a, float b) { return a / b; } }
拿走不谢!

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信