【发布时间】:2018-09-13 08:08:58
【问题描述】:
我正在编写一个程序,用户在其中输入 PC 的规格,然后我打印出这些规格。我有两个类,主类(PCSpecsDriver)和获取输入的类(PCSpecsClass)。
在 PCSpecsClass 中,我有一个名为“type”的重载方法,它基本上询问用户他们想要哪种类型的笔记本电脑或台式机。他们都有不同的参数。但是,当我尝试在驱动程序类中调用该方法时,它指向参数并给了我这个错误:“找不到符号”。
我知道这可能是一个简单的答案,但我似乎无法弄清楚。太感谢了!这是我的代码:
public class PCSpecsDriver {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String answer;
PCSpecsClass computer = new PCSpecsClass();
do {
System.out.println("What kind of PC would you like to build(Laptop/Desktop)?");
String response = kb.next();
switch (response.toLowerCase()) {
case "desktop":
computer.type(desktopResponse);
break;
case "laptop":
computer.type(laptopResponse);
default:
System.out.println("Not a valid response!");
}
System.out.println("Would you like to build another?");
answer = kb.next();
} while (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"));
}
public class PCSpecsClass {
Scanner kb = new Scanner(System.in);
String laptopResponse;
int desktopResponse;
public int type(int desktopResponse) {
System.out.println("Type: Input 1 for laptop, 2 for notebook, 3 for tablet");
return desktopResponse = kb.nextInt();
}
public String type(String laptopResponse) {
System.out.println("Size: Input 1 for mini, 2 for medium, 3 for large");
return laptopResponse = kb.nextLine();
}
【问题讨论】:
-
您提供的代码无法编译。请提供相关代码进行分析。
-
computer.desktopType(desktopResponse);这是你的问题。PCSpecsClass类没有desktopType方法。 -
@LuiggiMendoza 他们似乎在问为什么它不能编译。如果它编译,他们就不会在这里。
-
@mypetlion 没有名为
desktopResponse的变量,也没有laptopResponse。如果是这种情况,那么不值得花时间审查它,并且应该关闭这个问题,因为“这个人甚至没有审查 IDE 关于代码的内容”。 -
@mypetlion 抱歉,我才意识到我的错误。我将两个方法名称都更改为“类型”并相应地编辑了我的帖子。但是,代码仍然无法编译:/
标签: java class methods parameters parameter-passing