我最近一直试图解决涉及对象数组的问题,而我似乎无法解决“对象范围”的问题。
我有以下代码,并在这些行上:
ccaA[n].outputInfo("CCA");
和
clientA[n].outputInfo("Client");
我收到以下错误:
clientA cannot be resolved to a variable
让我困惑,因为它不是10行以上。
熟练的Java程序员可以向我解释一下吗?
public class MakePersonV2 {
public static void main(String[] args) {
int n; // Index number
int ccaNum, clientNum; // Number of Clients & CCA's to be created
// introduction
System.out.println("This main program creates a client and CCA object, "
+ "ensures their data\nfields are assigned values, and finally "
+ "outputs those values.");
ccaNum = PersonInfoV2.inputQuestion("CCA");
clientNum = PersonInfoV2.inputQuestion("Client");
for (n = 0; n <= ccaNum; n++)
{
// create a CCA and input CCA's information
System.out.println("\n\nCreating a CCA and entering information...");
PersonInfoV2[] ccaA = new PersonInfoV2[n]; // create CCA Object Array
ccaA[n].inputName(); // assign a name
ccaA[n].inputAge(); // assign an age
ccaA[n].inputGender(); // assign a gender
}
// create a client and input client's information
for (n = 0; n <= clientNum; n++)
{
System.out.println("\n\nCreating a client and entering information...");
PersonInfoV2[] clientA = new PersonInfoV2[n];// create Client Object Array
clientA[n].inputCCAName(); // assign a name
clientA[n].inputAge(); // assign an age
clientA[n].inputGender(); // assign a gender
}
// output client and CCA information
for (n = 0; n <= clientNum; n++)
{
ccaA[n].outputInfo("CCA"); // output CCA information
}
for (n = 0; n <= clientNum; n++)
{
clientA[n].outputInfo("Client"); // output Client information
}
} // main
} // end MakePerson