gpt4 book ai didi

java - 我无法引用我实例化的对象

转载 作者:行者123 更新时间:2023-12-01 23:11:20 25 4
gpt4 key购买 nike

我试图在 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 类 公开课学生数据 { 公共(public)字符串名称; 公共(public)字符串 ID; 公共(public) float 平均绩点;

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

最佳答案

这是 studentGPA 变量。它在本地 block 中声明:

if(!YES.equals(idPrompt))
{
// variable *declared* here!
StudentData aStudent = new StudentData(name, "", 0);
// aStudent is visible here
}
// but it's not visible here out of the scope of this local block.

因此仅在该 block 中可见。如果您希望它在整个方法中可见,请在方法或类范围中声明它。

关于java - 我无法引用我实例化的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21924439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com