【发布时间】:2014-10-22 21:32:54
【问题描述】:
我们的任务是获取老师给我们的代码,并在单独的 java 文件中编写该代码调用的方法。之后,我们应该把它放在同一个文件夹中。这是他给我们的代码:
import java.util.Scanner;
public class CSCD210Lab9
{
public static void main(String [] args)
{
Scanner kb = new Scanner(System.in);
String name = null;
double height, weight;
do
{
name = Lab9Methods.readName(kb);
height = Lab9Methods.readInfo(kb, "height");
weight = Lab9Methods.readInfo("weight", kb);
Lab9Methods.displayResults(name, height, weight, Lab9Methods.calcBMI(height, weight));
}while(Lab9Methods.goAgain(kb));
}// end main
}// end class
我已经编译了我在其中编写方法的文件,它编译得很好,完全没有错误。但是,当我马上去编译这段代码时,编译器指向每个编写 Lab9Methods 的实例,并给出错误“错误:找不到符号”。
我不明白为什么它不会从另一个文件中读取方法,即使两个文件都在同一个文件夹中。难道我做错了什么?
编辑
这是我的 Lab9Methods 类的代码。
import java.util.Scanner;
public class Lab9Methods
{
public static String readName(final Scanner kb)throws Exception
{
if(kb == null)
throw new RuntimeException("Precondition not met");
String name;
System.out.println("Please enter the person's name: ");
name = kb.nextLine();
return name;
}
public static double readInfo(final Scanner kb, final String str)
{
System.out.println("Please enter your" + str + ";");
double height = kb.nextDouble();
return height;
}
public static double readInfo(final String str ,final Scanner kb)
{
System.out.println("Please enter your" + str + ";");
double weight = readInfo(kb,str);
return weight;
}
public static int calcBMI(double height, double weight)
{
int calcBMI = (int)(weight/((height * height) * 703));
return calcBMI ;
}
public static void displayResults(final String name, final double height, final double weight, final double bmi)
{
System.out.println(name + "with a weight of" + weight + "and a height of" + height + " " + bmi);
if(bmi < 18.5)
{
System.out.println("You are underweight");
}
else if(bmi > 18.6 && bmi < 24.9)
{
System.out.println("You are normal");
}
else if(bmi > 25.1 && bmi < 29.9)
{
System.out.println("You are overweight");
}
else if(bmi > 29.9)
{
System.out.println("You are obese");
}
}
public static boolean goAgain(final Scanner kb)
{
boolean goAgain = false;
String answer;
System.out.println("Do you want to go again (Yes/No) :");
answer = kb.nextLine();
while(!answer.equals("yes") && !answer.equals("no"))
{
System.out.print("Error re-try, Do you want to go again (Yes/No) :");
answer = kb.nextLine();
}
if(answer.equals("yes"))
{
goAgain = true;
}
else if(answer.equals("no"))
{
goAgain = false;
}
return goAgain;
}
}
【问题讨论】:
-
如果可能的话,你能不能把 Lab9Methods 的类结构也贴出来。
-
是的,我会马上解决的。
-
现在,您是否也可以添加您用于编译和运行的命令。以及您执行命令的工作目录和创建类文件/源的目录。
-
我在 JGrasp IDE 中编译了它们,它们都在一个文件夹中,文件路径为“C:\Users\kcollins\Desktop\New folder”减去引号。
-
供参考,要编译,您可以使用:javac *.java,运行此命令后,应该为您的两个java文件生成.class文件。然后您可以使用以下命令运行应用程序:java CSCD210Lab9