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

时间:2014-02-21 02:59:36

标签: java object reference

我正在尝试在我的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 name;     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;
}

3 个答案:

答案 0 :(得分:2)

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

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.

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

答案 1 :(得分:1)

范围,范围,范围!! Java(和C / C ++)中的声明被“限定”为“块”,其中“块”是{}之间的一组语句。

这意味着如果你声明一个变量{ StudentData aStudent = new StudentData(...); },那么当你传递结束}时它变为“poof”。如果您希望它在该范围之外“可见”,则需要在块外声明aStudent

请注意,您仍然可以在内部执行分配,只是不要错误地包含StudentData,否则您将声明一个完全不同的变量版本。请注意,在Java中,必须在从声明到其使用的所有可能路径中为变量分配值,因此您可能必须将null(例如)分配给声明它的变量。

答案 2 :(得分:0)

对于您发布的问题,您可以在主要课程中执行此操作,

  // your code

  StudentData aStudent = null;

  while(!QUIT.equals(name))
  {
      //your code

      //if the user says yes
      if(!YES.equals(idPrompt))
      {
          //instantiate a new Student with just the name
           aStudent = new StudentData(name, "", 0);
      }
      else
      {
          //your code

          //instantiate a new Student with all three data types
          aStudent = new StudentData(name, studentID, studentGPA);
      }

    //your code