-3

私はJavaにかなり慣れていないので、これが私の最初のプログラミング課題です。私たちの目的は、2つの異なるクラス(学生、成績)に3人の学生の成績を実装し、平均を見つけることです。Student クラスと Grade クラスに次のメソッドを実装します。
Student クラス:

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

program= を実行すると、次のエラーが表示されます。

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

このプログラムには多くのコンパイル エラーがあります。コンパイル エラーのあるプログラム (Eclipse など) を実行しようとすると、例外が発生します。

コンパイル エラーの 1 つを次に示します。

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

Java では、識別子は大文字と小文字が区別されるためkeyboard、 とKeyboardは同じ識別子ではありません。

Keyboard別のケースでは、宣言でさえない場所で使用しようとしたことに気付きました。


コンパイル エラーから離れて、入力を処理するより良い方法は、一度Scannerオブジェクトを作成し、 (必要に応じて) メソッド パラメーターとしてドメイン クラスのメソッドに渡すことです。メソッド パラメーターとして渡すことができない場合 (メソッド シグネチャで許可されていないため)、コンストラクター パラメーターとして渡すか、(puke 1 )メイン クラスで変数を宣言します。mainpublic static

1 - そのような変数を使用することは悪い習慣であると教えられる (または教えられるべき) がstatic、現在、説明がおそらく意味をなさない学習段階にある.


しかし、主な教訓は、コードを実行する前にすべてのコンパイル エラーを修正する必要があるということです。

于 2013-09-26T09:36:23.037 に答える
2

これは、Keyboard クラスをインポートしていないためだと思います。ここからインポートに関する情報を入手できます。

于 2013-09-26T09:33:26.087 に答える
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 に答える