java多类程序
您好,我试图创建一个计算器,可以进行加法、减法、乘法和除法运算来挑战我自己,但我发现自己被困在开关部分:(我将指出开关消息中的错误,即“类型加法等中的方法加法等(字符串[])不适用于参数()我相信问题在于其他阶级的公共空虚
脚本:
public class ComputronCalc {
public static void main(String args[]) {
int mode;
mode = 1;
Addition ADD = new Addition();
Subtraction SUB = new Subtraction();
Multiplication MUL = new Multiplication();
Division DIV = new Division();
System.out.println("Hello welcome to the Computron fully functional calculator, coded by Samuel Cole, designed by Dwight Schrute.");
switch(mode) {
case 1:
ADD.Addition();<-----------addition is underlined in red
break;
case 2:
SUB.Subtraction();<-------------same
break;
case 3:
MUL.Multiplication();<---------------same
break;
case 4:
DIV.Division();<----------------same
break;
default:
System.out.println("You have not selected a mode, do so by editing the mode variable in the source.");
}
System.out.println("Thank you for choosing Computron.");
}
}
import java.util.Scanner;
public class Addition {
public void Addition(String Args[]) {
Scanner input = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Type the first number you desire to calculate.");
fnum = input.nextDouble();
System.out.println("Type the second number you desire to calculate.");
snum = input.nextDouble();
System.out.println("Calculating...");
answer = fnum + snum;
System.out.println(answer);
}
}
import java.util.Scanner;
public class Multiplication {
public void Multiplication(String Args[]) {
Scanner input = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Type the first number you desire to calculate.");
fnum = input.nextDouble();
System.out.println("Type the second number you desire to calculate.");
snum = input.nextDouble();
System.out.println("Calculating...");
answer = fnum * snum;
System.out.println(answer);
}
}
import java.util.Scanner;
public class Division {
public void Division(String Args[]) {
Scanner input = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Type the first number you desire to calculate.");
fnum = input.nextDouble();
System.out.println("Type the second number you desire to calculate.");
snum = input.nextDouble();
System.out.println("Calculating...");
answer = fnum / snum;
System.out.println(answer);
}
}
注意:我使用的是eclipse,所以每个类都位于不同的页面上
# 1 楼答案
虽然尖锐的问题是,您需要更改方法签名以不包含任何参数,或者更改方法调用以发送参数,但我认为有一个更好的解决方案,您应该使用concider
将操作类中的方法更改为构造函数:
然后,无需为每次运行实例化所有操作,开关将变为:
# 2 楼答案
您的方法需要参数“args”,而您不使用它。把它拿走。例如:
变成:
(顺便说一句,您的代码不遵循Oracle Java Naming Convention)
# 3 楼答案
四个运算符类中没有函数的任何参数(在本程序中为String[]args参数)。你的程序应该在没有它们的情况下运行
其他课程也是如此