我正在开发一个项目,该项目创建一个部分填充的Student类型数组,该数组永远不会超过100,000个Student对象。我正在尝试编写“list”部分,该部分应打印出所有学生,无论数组的顺序如何。
由于静态引用问题,我无法进行编译。我正在尝试使用已在第二个文件中创建的print()方法,但我无法这样做。
这是我到目前为止的代码:
import java.util.Scanner; // for keyboard access
public class Lab6 {
final int LENGTH = 100001;
private Student[] student = new Student[LENGTH];
private int currSize;
/**
* promptMenu - dumps the menu to the screen and gets the user's selection
* from the Scanner (usually keyboard), returning it
* @param usingInputStream - where to place the menu text (usually
* @return caharacter representing the user's (lowercased) selection
*/
public static char promptMenu(Scanner usingInputStream)
{
// dump the menu ...
System.out.println("Choose one:");
System.out.println(" (L)ist all students.");
System.out.println(" (A)dd a student.");
System.out.println(" (R)ead many students from file.");
System.out.println(" Sort Students by (F)inal exam score.");
System.out.println(" Sort Students by final a(V)erage.");
System.out.println(" Sort Students by (N)ame.");
System.out.println(" (Q)uit.");
// get the user's request ...
System.out.print("===>");
return Character.toLowerCase(usingInputStream.next().charAt(0));
}
public Lab6()
{
student = new Student[LENGTH];
}
public static void main(String[] args) {
// allow for keyboard input
Scanner kbd = new Scanner(System.in);
char choice; // holds the user's chosen option
// repeat until the user quits.
do
{
// print menu and get user's selection form menu
choice = promptMenu(kbd);
// based on user's choice, do something ....
switch(choice)
{
/***************************************************************************
* YOU WILL WANT TO ADD CODE FOR EACH OF CASES l, a, r, f, v, and n !!!!! *
***************************************************************************/
case 'l': // list array contents in current ordering (if any)
if(student != null)
{
for(int i=0; i<currSize; i++)
student.toString();
System.out.print(student);
}
else
System.out.println("The array is empty.");
break;
case 'a': // prompt user to add a single student and data from the keyboard
System.out.print("Please add a single student and data from the keyboard: ");
student = new Student[kbd.nextInt()];
for(int i=0; i< student.length; i++)
student[i] = kbd.nextInt();
break;
case 'r': // read data from file
break;
case 'f': // sort data by final exam score (numerically high to low)
break;
case 'v': // sort data by final average (numerically high to low)
break;
case 'n': // sort data by student name (alphabetically low to high)
break;
case 'q': // do nothing ...
break;
default: // uh, oh ... user gave a bad input.
System.out.println("Bad choice ("+choice+") try again!");
}
}
while (choice!='q'); // if user selected quit, then get out of loop.
// print terminating message (so user knows program has not "hung".
System.out.println("Thank you for using student record keeper!");
}
}
//以下是我在单独文件中提供的其他方法:
import java.util.Scanner; //code uses Scanner class
public class Student {
private String name; // student name (no spaces)
private int exam1; // student exam #1 score
private int exam2; // student exam #2 score
private int finalExam;// student final exam score
/**
* Constructor for Student class, given a student's pertinent data
* @param studentName - first name of student, as a String.
* @param exam1Score - student's score on exam #1
* @param exam2Score - student's score on exam #2
* @param finalExamScore - student's score on final exam
*/
public Student(String studentName,
int exam1Score, int exam2Score, int finalExamScore)
{
name = new String(studentName); // don't want reference copy
exam1 = exam1Score;
exam2 = exam2Score;
finalExam = finalExamScore;
}
/**
* Constructor for Student class, to be read from a Scanner. The Scanner
* could be the keybard or an open file, etc...
*
* @param s - the Scanner from which to read a student's data.
*
* NOTE: very important that data is provided in the order:
* name exam1 exam2 final
*/
public Student(Scanner s)
{
// for each instance varaiable, just read the associated data from the Scanner
name = s.next();
exam1 = s.nextInt();
exam2 = s.nextInt();
finalExam = s.nextInt();
}
// Accessors
/**
* getName
* @return the Student's name
*/
public String getName() {return name;}
/**
* getExam1
* @return the Student's score on exam #1
*/
public int getExam1() {return exam1;}
/**
* getExam2
* @return the Student's score on exam #2
*/
public int getExam2() {return exam2;}
/**
* getFinal
* @return the Student's score on the final exam
*/
public int getFinal() {return finalExam;}
/**
* getAvergae
* @return the Student's overall average
*/
public double getAverage()
{
double avg = (2.0/7)*exam1 + (2.0/7)*exam2 + (3.0/7)*finalExam;
return avg;
}
// Mutators
/**
* setName
* @param toName the new name for the student
*/
public void setName(String toName) {name=new String(toName);/*again, don't want reference copy*/}
/**
* setExam1
* @param toScore the new exam1 score
*/
public void setExam1(int toScore) {exam1=toScore;}
/**
* setExam2
* @param toScore the new exam2 score
*/
public void setExam2(int toScore) {exam2=toScore;}
/**
* setFinal
* @param toScore the new final exam score
*/
public void setFinal(int toScore) {finalExam=toScore;}
//utility methods
/**
* toString
* @return String representing the Student data. Suitable for use when listing students.
*
* note that adding a toString() method to any class allows you to use an object of that
* anywhere a String could go. The most common place for such is when printing, and
* the result would be that what is printed is what is returned by the toString method.
* So, the following code:
* Student almostPerfect = new Student("Perfection", 99, 98, 100);
* System.out.println(almostPerfect);
* would print something like:
* Perfection: 99 98 100 99.14
*/
public String toString()
{
String result = String.format("%40s:%5d%5d%5d%8.2f",
name, exam1, exam2, finalExam,
getAverage());
return result;
}
/**
* print - simply dumps the STudent to the screen, using format found above in toString()
*/
public void print()
{
System.out.print(this);
}
}
答案 0 :(得分:0)
问题是你试图在静态方法(主方法)中使用非静态字段(学生):
student = new Student[kbd.nextInt()];
在这种情况下,你必须使你的变量学生成为静态变量,所以主要方法可以使用if。
私人静态学生[]学生=新学生[长度];
静态和非静态变量和方法有一些简单的规则:
1.静态方法只能访问静态变量(例如使用static修饰符声明的静态变量)。主要方法是ALWAYS静态,因此您打算在其中使用的任何变量必须声明为static。
2.非静态方法可以访问静态和非静态两种类型的变量。