2014-02-21 76 views
0

我想引用我的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; 
} 
+0

@Makoto:這不是真的需要。這是關於在本地塊中聲明一個變量,限制它的範圍。 –

回答

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. 

因此只在該塊中可見。如果您希望在整個方法中都可見,請在方法或類範圍內聲明它。

+0

感謝您的幫助,我最終實例化了If語句之外的對象。如果我的老師曾經解釋過這些東西,這會很有幫助! – user3335456

+1

@ user3335456:你的老師無法解釋所有事情,因爲太多解釋和學習。通過絆倒這些錯誤,你實際上學到了很多東西。取得的錯誤=獲得的知識。 –

+0

非常真實。現在看來非常明顯,我得到了一些幫助。感謝您的正確方向。 – user3335456

1

範圍,範圍,範圍! Java(和C/C++)中的聲明是「範圍」到「塊」,其中「塊」是{}之間的一組語句。

這意味着如果您聲明變量{ StudentData aStudent = new StudentData(...); },那麼當您通過關閉}時,它會變「poof」。如果您希望它在該範圍之外「可見」,則需要在塊外部聲明aStudent

請注意,您仍然可以在內部完成的分配,只是不要錯誤地包含StudentData或者您將聲明完全不同的變量版本。並且請注意,在Java中,變量必須爲從其聲明到使用的所有可能路徑分配一個值,因此您可能必須將null(例如)分配給聲明的變量。

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 
相關問題