[英]Java error in my 2nd class
I have 2 classes. 我有2节课。 1 class sets & gets a students' name, 3 test scores for a student & an average for the 3 scores. 1个班级设置并获得一个学生的名字,一个学生获得3个测试成绩,并获得3个成绩的平均值。 The other class has a main method to set and get this information. 另一个类具有设置和获取此信息的主要方法。 I'm getting an error from my IDE for the 2nd class only. 我从IDE中仅收到第二类错误。
public class Student5th
{ /**
Instance variables
Each student object will have a name and three test scores
*/
private String name; //Student name
private int test1; //Score on test 1
private int test2; //Score on test 2
private int test3; //Score on test 3
/**
Set a student's name
Preconditions -- nm is not empty
Postconditions -- name has been set to name
*/
public void setName (String nm)
{
name = nm;
}
/** Get a student's name
Preconditions -- none
Postconditions -- returns the name
*/
public String getName ()
{
return name;
}
/** Set the score on the indicated test
Preconditions -- 1 <= i <= 3
-- 0 <= score <= 100
Postconditions -- test i has been set to score
*/
public void setScore (int i, int score)
{
if (i == 1) test1 = score;
else if (i == 2) test2 = score;
else test3 = score;
}
/** Get the score on the indicated test
* Preconditions -- none
* Postconditions -- returns the score on test I
* */
public int getScore (int i)
{
if (i == 1) return test1;
else if (i == 2) return test2;
else return test3;
}
/** Compute and return a student's average
* Preconditions -- none
* Postconditions -- returns the average of the test scores
* */
public int getAverage() {
int average;
average = (int) Math.round((test1 + test2 + test3) / 3.0);
return average;
}
}
My 2nd class with the main method... 我的第二类主要方法...
import java.util.*;
public class TestStudent
{
public static void main (String[] args)
{
Scanner console = new Scanner(System.in);
private Student **student1**;
private Student **student2**;
String s;
int t;
student1 = new Student();
student2 = new Student();
s = console.next();
student1.setName (s);
t = console.nextInt();
student1.setScore (1, t);
student1.setScore (2, console.nextInt());
student1.setScore (3, console.nextInt());
student2.setName (**keyboard**.readLine());
student2.setScore (1, console.nextInt());
student2.setScore (2, console.nextInt());
student2.setScore (3, console.nextInt());
}
}
I've bolded (well, put double asterisks around) the parts which are giving me errors. 我已经加粗了(好了,用双星号括起来)那些给我错误的部分。 I'm close to getting this program to work, but I don't know why my IDE is giving me problems for student1 & student2 in the 2nd class, as well as giving me a problem for (keyboard.readLine()); 我快要开始运行该程序了,但是我不知道为什么我的IDE在第二课中给Student1和Student2带来了问题,也给我(keyboard.readLine())带来了问题。 for student2.setName in the 2nd class? 第二课的student2.setName?
You shouldn't specify an access level modifier (like private
or public
) inside a method: 您不应该在方法内部指定访问级别修饰符(例如private
或public
):
Student student1; // delete 'private'
Student student2; // delete 'private'
Why? 为什么? Because if you declare a variable inside a method, it should only be visible inside that specific method. 因为如果在方法内部声明变量,则该变量仅应在该特定方法内部可见。 It doesn't make sense to declare it as private
, public
or protected
. 将其声明为private
, public
或protected
没有意义。
You could take a look to this article about Information hiding . 您可以看一下有关信息隐藏的文章。
A few things going on here. 这里发生了一些事情。
If your lecturer specified that student1
and student2
must be private
, then he intended them to fields of the class. 如果您的讲师指定student1
和student2
必须是private
,那么他打算将他们private
到班级的领域。 That means you have to declare them outside of the method. 这意味着您必须在方法之外声明它们。 Move these two declarations up, to before the line that says public static void main ...
. 将这两个声明上移至表示public static void main ...
的行之前。
Also, your class is called Student5th
, but you're using it within TestStudent
as if it were just Student
. 另外,您的班级称为Student5th
,但是您正在TestStudent
中使用它,就好像它只是Student
。 The class name used in the variable declarations has to match the actual name of the class. 变量声明中使用的类名称必须与该类的实际名称匹配。
Where you wrote keyboard.readLine()
towards the end, you actually meant to write ccnsole.nextLine()
. 在最后写出keyboard.readLine()
的地方,实际上是要写ccnsole.nextLine()
。 You shouldn't be trying to read the same input stream with two different objects at the same time. 您不应该尝试同时读取具有两个不同对象的相同输入流。
If you do change keyboard.readLine()
to console.nextLine()
, then you'll need an extra call to console.nextLine()
immediately before it, to consume the newline character at the end of the line that has the first student's third score. 如果您确实将keyboard.readLine()
更改为console.nextLine()
,那么您将需要在其之前立即额外调用console.nextLine()
,以在第一位学生所在行的末尾使用换行符第三得分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.