我是一个相当新的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的新手。
答案 0 :(得分:2)
我认为那是因为你没有导入Keyboard类,你可以从here
获取导入内容答案 1 :(得分:2)
此程序中存在许多编译错误。如果您尝试运行具有编译错误的程序(例如在Eclipse中),您将获得异常
编译错误就在这里:
Scanner keyboard = new Scanner(System.in);
...
programGrade = Keyboard.readInt();
在Java中,标识符区分大小写,因此keyboard
和Keyboard
不是相同的标识符。
在另一个案例中,我注意到您曾尝试在甚至不是声明的地方使用Keyboard
。
退出编译错误,处理输入的更好方法是在Scanner
中创建main
对象一次,然后将其传递给方法您的域类(根据需要)作为方法参数。如果你不能将它作为方法参数传递(因为方法签名不允许),你可以将它作为构造函数参数传递或(puke 1 )声明public static
主类中的变量。
1 - 您将(或应该)被教导使用static
这样的变量是不好的做法,但是您目前处于学习的阶段,其解释可能没有意义你
但主要的教训是,在尝试运行代码之前,应该纠正所有编译错误。
答案 2 :(得分: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)
在第16行的Student.java中,使用了单词Keyboard,但未知(“未解析”)。
大多数情况下,当import
缺失或发生拼写错误时会发生这种情况。
提示:有时IDE中不显示行号,然后查找设置以显示它们。
IDE也允许自动完成:输入“Ke”,然后按Ctrl-空格键选择。
答案 3 :(得分: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关闭。因此,在您的情况下,类Student的方法setName()中出现异常,该异常在第0行的同一类的方法setup()中调用。 8 ......等等。
现在,如果你看到行号。 16个班级学生将在setName()方法中(最有可能)
fullName = Keyboard.readString();
此处无法识别Keyboard
。按照Java命名约定,它看起来像一个类。如果它是一个类,那么你必须导入它然后你可以调用它的静态函数readString()。如果它是变量的(首先将名称更改为驼峰大小写)然后实例化对象forst然后在其上调用方法。