我想引用我的StudentData類中的GPA,但是我收到一個錯誤,說它找不到符合表示aStudent的行。 gpa = studentGPA。我不能引用我已經實例化的對象
我的主類:
public static void main (String [] args)
{
//local constants
final String QUIT = "Quit";
final String YES = "Y";
final int MODIFY_GPA = 1;
final int DISPLAY_USER = 2;
final int QUIT_MENU = 3;
//local variables
String name; //name of the user
String idPrompt; //asks the user if they want to input a gpa and id
String studentID; //student ID input by the user
float studentGPA; //student GPA input by the user
int choice; //prompts the user for a menu choice
Library myLib = new Library();
/****************** Start main method *******************/
//prompt for name of user or quit
System.out.print("Enter name of user(First and Last, Quit to end): ");
name = Keyboard.readString();
while(!QUIT.equals(name))
{
//ask the user if they want to enter ID and GPA
System.out.print("Do you want to enter an ID and GPA?(Y/N): ");
idPrompt = Keyboard.readString();
//if the user says yes
if(!YES.equals(idPrompt))
{
//instantiate a new Student with just the name
StudentData aStudent = new StudentData(name, "", 0);
}
else
{
//prompt the user for the ID
System.out.print("Enter the Student ID: ");
studentID = Keyboard.readString();
//prompt the user for the GPA
System.out.print("Enter the Student GPA: ");
studentGPA = Keyboard.readFloat();
//instantiate a new Student with all three data types
StudentData aStudent = new StudentData(name, studentID, studentGPA);
}
//clear the screen
myLib.clrscr();
//prompt user for a menu choice
choice = displayMenu();
//clear the screen
myLib.clrscr();
//begin while loop to modify the user or display
while(choice != QUIT_MENU)
{
//if the user wants to modify the GPA
if(choice == MODIFY_GPA)
{
studentGPA = StudentData.modifyGPA();
aStudent.gpa = studentGPA;
}
//if the user wants to display
else if(choice == DISPLAY_USER)
{
System.out.print("STUDENT OUTPUT");
//System.out.println(StudentData);
}
//if there was an invalid menu choice
else
{
System.out.print("INVALID DATA ENTERED");
}
//prompt for next choice
choice = displayMenu();
}
//prompt for name of user or quit
System.out.print("Enter name of user(First and Last, Quit to end): ");
name = Keyboard.readString();
}
}//end main method
,這是我StudentData類 公共類StudentData { 公共字符串名稱; public String id; public float gpa;
//create a constructor that will receive the name of the student only
public StudentData(String inName)
{
//local variables
id = ""; //set the ID to null
gpa = 0.00F; //set the gpa to 0.00
name = inName; //gets the name of the student from the user
}
//create an overloaded constructor that will receive all three pieces of instance data
public StudentData(String inName, String inID, float inGPA)
{
name = inName; //name input by user through the constructor parameter
id = inID; //ID input by user through the constructor parameter
gpa = inGPA; //GPA input by user through constructor parameter
}
//create a method that will modify the students GPA
public static float modifyGPA()
{
//local constants
//local variables
float newGPA;
System.out.print("Enter new GPA: ");
newGPA = Keyboard.readFloat();
return newGPA;
}
//create a toString method that will format the instance data to look like:
public String toString()
{
String format;
format = ("Student" + name + "\n" +
"Student ID" + id + "\n" +
"Student GPA" + gpa + "\n");
return format;
}
@Makoto:這不是真的需要。這是關於在本地塊中聲明一個變量,限制它的範圍。 –