您好我正在尝试创建一个计算器,可以添加减法乘法和除法来挑战自己,但发现自己陷入了开关部分:(我将指出切换消息中的错误,说“方法添加等”(类型添加等中的String [])不适用于arguments()。“我认为问题在于其他类的公共空白。
脚本:
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,所以每个类都在不同的页面上。
答案 0 :(得分:3)
您的方法期望参数“args”,您不使用它。去掉它。例如:
public void Addition(String Args[]) {
变为:
public void Addition() {
(顺便说一句,您的代码不遵循Oracle Java Naming Convention)
答案 1 :(得分:0)
虽然严重的问题是您需要更改方法签名以不具有任何参数或更改方法调用以发送参数,但我认为有一个更好的解决方案,您应该更好。
将操作类中的方法更改为构造函数:
public class Addition {
public Addition() {
//...
}
}
然后您不需要为每次运行实例化所有操作,并且交换机变为:
switch(mode) {
case 1:
Addition();
break;
case 2:
Subtraction();
break;
case 3:
Multiplication();
break;
case 4:
Division();
break;
default:
System.out.println("You have not selected a mode, do so by editing the mode variable in the source.");
}
答案 2 :(得分:0)
对于四个运算符类中的函数,没有任何参数(在此程序中,String [] args参数)。你的程序应该没有它们。
public class Addition {
public Addition() {
//...
}
}
同样适用于其他类。
public class Subtraction {
public Subtraction() {
//...
}
}
public class Multiplication{
public Multiplication() {
//...
}
}
public class Division {
public Division() {
//...
}
}