-2

我们的作业目标是在两个不同的班级(学生,成绩)中实现 3 名学生的成绩,并找到我到目前为止的平均成绩,

  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)
    {
    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;
    }
    }

每当我尝试运行它时,我都会收到此错误:

线程“main”java.lang.Error 中的异常:未解决的编译问题:无法解析键盘

at Student.setName(Student.java:18)
at Student.setup(Student.java:10)
at Program01.main(Program01.java:19)

任何帮助将不胜感激。

4

3 回答 3

1

我想Keyboard你想Scanner在你的程序中使用。

在文件中包含以下行

import java.util.Scanner;

并创建它的一个实例。

Scanner keyboard = new Scanner(System.in);

或者你有一些其他的 API,它有一个名为的类,Keyboard然后将该文件导入你的程序。

注意:顺便说一下Scanner没有readInt()方法,它只有nextInt()

当您使用 Eclipse 作为 IDE 并尝试运行甚至无法编译的代码时,可能会发生此错误。检查 Eclipse 中的 Problems 视图,并在执行应用程序之前修复编译错误。

于 2013-09-26T07:49:59.657 回答
0

您似乎缺少键盘实用程序的导入。Java:使用键盘类输入字符串

尝试导入它,应该可以解决问题。

PS:这个已经在另一个话题中讨论过了。

于 2013-09-26T07:51:05.613 回答
-1

利用

 Scanner sc = new Scanner(System.in); 

而不是键盘

于 2013-09-26T07:46:27.133 回答