使用java类描述一个计算器类 计算器具备操作数1, 操作数2 ,还具备计算的功能行为。
答案:2 悬赏:10 手机版
解决时间 2021-03-30 10:13
- 提问者网友:留有余香
- 2021-03-29 09:34
使用java类描述一个计算器类 计算器具备操作数1, 操作数2 ,还具备计算的功能行为。
最佳答案
- 五星知识达人网友:归鹤鸣
- 2021-03-29 09:40
//给你给之前我些的代码
//封装两个数
public class Computer {
private double num1;
private double num2;
public Computer() {
}
public Computer(double num1, double num2) {
this.num1 = num1;
this.num2 = num2;
}
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
}
//封装加法运算
public class Sum {
public double method(Computer com){
return com.getNum1()+com.getNum2();
}
}
//封装减法运算
public class Subtraction {
public double method(Computer com){
return com.getNum1()-com.getNum2();
}
}
//封装乘法运算
public class Product {
public double method(Computer com){
return com.getNum1()*com.getNum2();
}
}
//封装除法运算
public class Divide {
public double method(Computer com){
return com.getNum1()/com.getNum2();
}
}
//封装运算过程
public class ComputerFactry {
public double method(int option,double num1,double num2){
double number=0;
Computer com=new Computer(num1,num2);
switch (option) {
case 1:
number= new Sum().method(com);
break;
case 2:
number= new Subtraction().method(com);
break;
case 3:
number= new Product().method(com);
break;
case 4:
number= new Divide().method(com);
break;
}
return number;
}
}
//程序界面
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
double num1=0,num2=0;
int option=0;
Scanner sca=new Scanner(System.in);
ComputerFactry cf=new ComputerFactry();
System.out.println(" "+"=====欢迎使用计算器=====");
System.out.println("1,加法运算(+)");
System.out.println("2,减法运算(-)");
System.out.println("3,乘法运算(×)");
System.out.println("4,除法运算(÷)");
System.out.print("请选择运算方法:");
option=sca.nextInt();
System.out.print("请输入第一个数:");
num1=sca.nextDouble();
System.out.print("请输入第二个数:");
num2=sca.nextDouble();
System.out.println(cf.method(option, num1, num2));
}
}
//封装两个数
public class Computer {
private double num1;
private double num2;
public Computer() {
}
public Computer(double num1, double num2) {
this.num1 = num1;
this.num2 = num2;
}
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
}
//封装加法运算
public class Sum {
public double method(Computer com){
return com.getNum1()+com.getNum2();
}
}
//封装减法运算
public class Subtraction {
public double method(Computer com){
return com.getNum1()-com.getNum2();
}
}
//封装乘法运算
public class Product {
public double method(Computer com){
return com.getNum1()*com.getNum2();
}
}
//封装除法运算
public class Divide {
public double method(Computer com){
return com.getNum1()/com.getNum2();
}
}
//封装运算过程
public class ComputerFactry {
public double method(int option,double num1,double num2){
double number=0;
Computer com=new Computer(num1,num2);
switch (option) {
case 1:
number= new Sum().method(com);
break;
case 2:
number= new Subtraction().method(com);
break;
case 3:
number= new Product().method(com);
break;
case 4:
number= new Divide().method(com);
break;
}
return number;
}
}
//程序界面
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
double num1=0,num2=0;
int option=0;
Scanner sca=new Scanner(System.in);
ComputerFactry cf=new ComputerFactry();
System.out.println(" "+"=====欢迎使用计算器=====");
System.out.println("1,加法运算(+)");
System.out.println("2,减法运算(-)");
System.out.println("3,乘法运算(×)");
System.out.println("4,除法运算(÷)");
System.out.print("请选择运算方法:");
option=sca.nextInt();
System.out.print("请输入第一个数:");
num1=sca.nextDouble();
System.out.print("请输入第二个数:");
num2=sca.nextDouble();
System.out.println(cf.method(option, num1, num2));
}
}
全部回答
- 1楼网友:英雄的欲望
- 2021-03-29 11:14
com.setnum1(800) 这样,下面也类似,不要再接=,括号里就是你要传的参数追问
这样报错
你说的这个 效果都一样的 (*^__^*)
这样报错
你说的这个 效果都一样的 (*^__^*)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯