我正在开发一个项目,该项目创建一个部分填充的 Student 类型数组,该数组永远不会超过 100,000 个 Student 对象。我正在尝试对“列表”部分进行编码,该部分应该以数组的顺序打印出所有学生。
由于静态引用问题,我无法进行编译。我正在尝试使用 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);
}
}
最佳答案
哦,亲爱的,我很抱歉你的代码真的很乱而且不合适。也许开始使用 IDE(Eclipse?)。
无论哪种方式,这一行都是问题
private Student[] student = new Student[LENGTH];
// it needs to be changed to static since its in the main method and that is a static
private static Student[] student = new Student[LENGTH];
尽管我真的建议您研究一下 static 修饰符的作用......以及如何正确初始化静态对象。
我将尝试快速概述静态修饰符,
1) 可以从任何其他类调用静态方法,具体取决于可见性限制(私有(private)、公共(public)、 protected 、默认)。要从具有正确访问权限的另一个类调用静态方法,您可以执行 NAMEOFCLASSWHEREMETHODIS.METHODNAME();
2) 静态方法/对象可以被调用,因为它不需要类的实例。因此不需要执行 CLASSNAME classn = new CLASSNAME();缺点是静态对象在启动时初始化。
我会继续,但那里有比我更好的指南。请查看它并继续努力编写您的代码!记得拿个ide,咳https://www.eclipse.org/downloads/
编辑: 在Student.java中
@Override
public String toString() {
return this.name + " " + this.score (and so on) ...;
}
然后你可以进去做类似的事情
System.out.println(student);
或者如果你想打印数组
for(Student s: student)
System.out.println(s)
关于java - 无法对非静态现场学生进行静态引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30034518/