-3

我对 Java 相当陌生,这是我的第一个编程任务,我们的目标是在两个不同的班级(学生,成绩)中实现 3 个学生的成绩并找到平均值。我们将在学生和年级类中实现以下方法:
学生类:

public class Student - Defines a student with a full name and a complete set of grades:

public void setup() - Sets all attributes of the student (name and grades).
private void setName() - Sets the name of the student.
private void setGrades() - Sets all the grades for the student.
public void display() - Displays the complete information on the student.
public double overallGrade() - Returns the overall grade of the student.

Grades class:
public class Grades - Defines a complete set of grades received by a student.

public void setup() - Sets the complete set of grades
public void display() - Displays the complete set of grades
public double average() - Returns the average of the complete set of grades (i.e., it returns a number between 0.0 and 100.0).

这是我的程序:

public class Program01 {

public static void main(String[] args)
{
Student bob, john, matt;
Grades grades; 

grades = new Grades();

double bobgrade, johngrade, mattgrade;

bob = new Student();
john = new Student();
matt = new Student();

bob.setup();
john.setup();
matt.setup();

bob.display();
john.display();
matt.display();

bobgrade = bob.overallGrade();
johngrade = john.overallGrade();
mattgrade = matt.overallGrade();

grades.average(bobgrade, johngrade, mattgrade);

System.out.println("The overall grade for the class is: " + grades.theSectionAverage);
}
}



public class Student {
Grades grades; 
String fullName, firstName, lastName, name;
int studentProgramGrade, studentExamGrade;

public void setup(){
setName();
setGrades();
}

public void setName()
{

System.out.print("Please, enter the student's name in the form of Doe, John or Smith, Jane:");
fullName = Keyboard.readString();

firstName = fullName.substring(fullName.indexOf(" ") + 1, fullName.length()); 
lastName = fullName.substring(0, fullName.indexOf(","));


name = firstName + " " + lastName;
}

public void setGrades()
{
studentExamGrade = grades.setupExam(name);
studentProgramGrade = grades.setupProgram(name);
} 

public void display()
{
System.out.println(name + " " + grades.display());
} 

public double overallGrade()
{
final double PROGRAM_WEIGHT = 0.40;
final double EXAM_WEIGHT = 1 - PROGRAM_WEIGHT;

double theOverallGrade;

theOverallGrade = studentProgramGrade * PROGRAM_WEIGHT + studentExamGrade * EXAM_WEIGHT;

return theOverallGrade;
}

}




public class Grades {
int programGrade, examGrade;
double theSectionAverage;

public int setupExam(String studentname)
{
System.out.print("Please, enter the exam grade for " + studentname + ":");
examGrade = Keyboard.readInt();

return examGrade;
}

public int setupProgram(String studentname)
{
Scanner keyboard = new Scanner(System.in);

System.out.print("Please, enter the program grade for " + studentname + ":");
programGrade = Keyboard.readInt();

return programGrade;
}

public String display()
{
return programGrade + " " + examGrade;
}

public double average(double bobgrade, double johngrade, double mattgrade)
{
theSectionAverage = bobgrade + johngrade + mattgrade / 3;

return theSectionAverage;
}
}

当我运行我的程序=时,它给了我以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Keyboard cannot be resolved

at Student.setName(Student.java:16)
at Student.setup(Student.java:8)
at Program01.main(Program01.java:17)

任何帮助将不胜感激,就像我说的,我是 Java 新手。

4

4 回答 4

2

我认为那是因为你没有导入 Keyboard 类,你可以从这里得到关于导入的东西

于 2013-09-26T09:33:26.087 回答
2

您在此程序中有许多编译错误。如果你试图运行一个有编译错误的程序(例如在 Eclipse 中),你会得到一个异常

编译错误之一在这里:

Scanner keyboard = new Scanner(System.in);
...
programGrade = Keyboard.readInt();

在 Java 中,标识符区分大小写,因此keyboardKeyboard不是同一个标识符。

在另一种情况下,我注意到您曾尝试Keyboard在甚至没有声明的地方使用。


从编译错误退后一步,处理输入的更好方法是创建一次Scanner对象,然后将其作为方法参数传递给域类的方法(根据需要)。如果你不能将它作为方法参数传递(因为方法签名不允许它),你可以将它作为构造函数参数传递或(puke 1 )在主类中声明一个变量。mainpublic static

1 - 您将(或应该)被教导使用这样的static变量是不好的做法,但您目前处于学习阶段,解释可能对您没有意义。


但主要的教训是,在尝试运行代码之前,您应该更正所有编译错误。

于 2013-09-26T09:36:23.037 回答
0
Exception in thread "main" java.lang.Error:
    Unresolved compilation problem: Keyboard cannot be resolved
at Student.setName(Student.java:16)
at Student.setup(Student.java:8)
at Program01.main(Program01.java:17)

在 Student.java 的第 16 行中,使用了单词 Keyboard,但它是未知的(“未解决”)。大多数情况下,这发生在import缺少或出现拼写错误时。

提示:有时 IDE 中不显示行号,然后查找设置以显示它们。

IDE 也将允许自动完成:输入“Ke”,然后按 Ctrl-Space 进行选择。

于 2013-09-26T09:34:35.490 回答
0

如果您是 Java 新手,请学习了解堆栈跟踪。从长远来看会对你有很大帮助。在你的情况下

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Keyboard cannot be resolved

at Student.setName(Student.java:16)
at Student.setup(Student.java:8)
at Program01.main(Program01.java:17)

它说Student类的函数setName()中发生了异常,对应的行号是16。现在你必须看到这一行来检查究竟是什么导致了异常。

只是为了记录,堆栈跟踪将为您提供异常传播树,直到它被捕获或如果没有被捕获,最终传播到导致 JVM 关闭的主。因此,在您的情况下,Class Student 的方法 setName() 发生了异常,该方法在同一类的 setup() 方法中被调用。8……等等。

现在,如果您看到行号。16 名学生将在 setName() 方法中(最有可能)

fullName = Keyboard.readString();

这里不承认Keyboard。按照 java 命名约定,它看起来像一个类。如果它是一个类,那么你必须导入它,然后你可以调用它的静态函数 readString()。如果它是可变的(首先将名称更改为驼峰大小写)然后实例化对象 forst 然后调用它的方法。

于 2013-09-26T09:43:23.620 回答