【问题标题】:I can't reference the object I've instantiated我无法引用我实例化的对象
【发布时间】:2014-02-21 02:59:36
【问题描述】:

我试图在我的 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 { 公共字符串名称; 公共字符串 ID; 公共浮动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:这并不是真正需要的。这就是在本地块中声明一个变量,限制它的范围。

标签: java object reference


【解决方案1】:

这是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.

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

【讨论】:

  • 感谢您的帮助,我最终在 If 语句之外实例化了该对象。如果我的老师能解释这些事情会很有帮助!
  • @user3335456:你的老师无法解释一切,因为要解释和学习的东西太多了。实际上,您可以通过绊倒这些错误来学到很多东西。犯的错误 = 获得的知识。
  • 非常正确。现在似乎很明显,我得到了一些帮助。感谢您朝着正确的方向推动。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    对于您发布的问题,您可以在主类中这样做,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 2016-01-18
      • 2018-10-04
      • 2013-03-06
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多