Java 我不知道我的程序出了什么问题,
我对java相当陌生,这是我的第一个编程作业,我们的目标是在两个不同的班级(学生,年级)实施3个学生的成绩,并找出平均成绩。 我们将在学生和年级班级实施以下方法:Java 我不知道我的程序出了什么问题,,java,Java,我对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
学生班级:
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新手。我想这是因为您没有导入键盘类,您可以从 在Student.java的第16行中,使用了单词Keyboard,但它是未知的(“未解析”)。 这种情况通常发生在
导入
丢失或出现打字错误时
提示:有时IDE中不显示行号,然后查找显示行号的设置
IDE还允许自动完成:键入“Ke”,然后按Ctrl空格键进行选择。此程序中存在大量编译错误。如果您试图运行一个有编译错误的程序(例如在Eclipse中),您将得到一个异常 以下是编译错误之一:
Scanner keyboard = new Scanner(System.in);
...
programGrade = Keyboard.readInt();
在Java中,标识符区分大小写,因此键盘
和键盘
不是相同的标识符
在另一个例子中,我注意到您试图在甚至没有声明的情况下使用键盘
从编译错误中退一步,处理输入的更好方法是在main
中创建一次Scanner
对象,然后将其作为方法参数传递给域类的方法(根据需要)。如果不能将其作为方法参数传递(因为方法签名不允许),则可以将其作为构造函数参数传递,或者(puke1)在主类中声明一个public static
变量
1-您将(或应该)被告知,使用像这样的静态
变量是不好的做法,但您目前正处于学习阶段,其中的解释可能对您没有意义
但主要的教训是,在尝试运行代码之前,您应该纠正所有编译错误。如果您是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。现在,您必须看到这一行,以检查导致异常的实际原因
仅针对记录stacktrace将为您提供异常传播树,直到它被捕获为止,或者如果没有被捕获,则最终传播到主引导,导致JVM关闭。因此,在您的案例中,类Student的方法setName()发生异常,该方法在第8行的同一类的方法setup()中调用。。。等等
现在,如果你看到student类的第16行,它将在setName()中(很可能是)
这里它无法识别键盘
。按照java命名约定,它似乎是一个类。如果它是一个类,则必须导入它,然后可以调用它的静态函数readString()。如果它是可变的(首先将名称更改为camel case),则为ST实例化对象,然后对其调用方法。可能重复的
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)
fullName = Keyboard.readString();