我正在上大学暑期课,速度超快,而且我对编程一般都很新(很好哈哈)。
本周我们的一项任务是创建一个简单的计算器。以下是一些说明。
“Driver类是唯一具有main(String [] args)方法的类.Driver类应调用Menu类中的方法来打印菜单,并且从该方法应该调用Actions类中的方法菜单中的相应功能。“
我的代码没有接近完成我只是试图弄清楚我在调用Menu类中的getInput方法时遇到了什么问题。根据说明,方法调用似乎需要在Driver类中。但是,当我尝试编译时,我遇到了一些错误。
Driver.java:6: error: cannot find symbol
String user = getInput("Enter: \n 1=Add \n 2=Subtract \n 3=Multiply \n 4=Divide");
^
symbol: method getInput(String)
location: class Driver
Driver.java:11: error: cannot find symbol
String s1 = getInput("Enter a numeric value: ");
^
symbol: method getInput(String)
location: class Driver
Driver.java:12: error: cannot find symbol
String s2 = getInput("Enter a numeric value: ");
^
symbol: method getInput(String)
location: class Driver
Driver.java:14: error: non-static variable user cannot be referenced from a static context
int userInt = Integer.parseInt(user);
^
Driver.java:70: error: Illegal static declaration in inner class Driver.Menu
public static String getInput(String prompt) {
^
modifier 'static' is only allowed in constant variable declarations
5 errors
................
import java.util.Scanner;
import java.io.*;
public class Driver {
String user = getInput("Enter: \n 1=Add \n 2=Subtract \n 3=Multiply \n 4=Divide");
public static void main(String[] args) {
String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");
int userInt = Integer.parseInt(user);
double result = 0;
switch (userInt) {
case 1:
result = addValues(s1, s2);
break;
case 2:
result = subtractValues(s1, s2);
break;
case 3:
result = multiplyValues(s1, s2);
break;
case 4:
result = divideValues(s1, s2);
break;
default:
System.out.println("You entered an incorrect value");
}
System.out.println("The answer is " + result);
}
private static double divideValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 / d2;
return result;
}
private static double multiplyValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 + d2;
return result;
}
private static double subtractValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 - d2;
return result;
}
private static double addValues(String s1, String s2)
throws NumberFormatException {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 + d2;
return result;
}
public class Menu {
public static String getInput(String prompt) {
String user;
Scanner scan = new Scanner(System.in);
System.out.println(prompt);
return user = scan.nextLine();
}
}
}
这篇文章已经很长了,所以除非你愿意,否则我不会发布其余的说明。
答案 0 :(得分:2)
getInput()
是另一个类中的静态方法,您应该按如下方式调用它:
String s1 = Menu.getInput("Enter a numeric value: ");
// ^ the name of the class should come before the method name
答案 1 :(得分:0)
javadoc 表示 - 静态方法,在其声明中有静态修饰符,应该使用类名调用,而不需要创建类的实例,如
ClassName.methodName(args)
所以你必须改变
String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");
到
String s1 = Menu.getInput("Enter a numeric value: ");
String s2 = Menu.getInput("Enter a numeric value: ");
此外,您还可以使用像
这样的对象引用来引用静态方法instanceName.methodName(args)
修改:
菜单类
class Menu {
public static String getInput(String prompt) {
String user;
Scanner scan = new Scanner(System.in);
System.out.println(prompt);
return user = scan.nextLine();
}
}
驱动程序类
public class Driver {
public static void main(String[] args) {
String user = Menu.getInput("Enter: 1=Add 2=Subtract 3=Multiply 4=Divide");
String s1 = Menu.getInput("Enter a numeric value: ");
String s2 = Menu.getInput("Enter a numeric value: ");
//rest of the codes same
}
}