我正在做一个使用 OOP 的作业。该程序本质上将根据用户输入的内容创建一堆不同的狐猴。它们都具有 Parent 类的相似特征以及它们自己类中的独特特征。
该程序的一部分是允许用户决定要创建的对象(或狐猴)的数量。我想以渐进的方式创建对象,所以 L1、L2、L3 ......等等。
这是我到目前为止所拥有的。所以我基本上想使用 lemCounter 来跟踪狐猴编号并将其附加到对象名称,每次创建新对象时。
//Main section of code
static int numLems, typeLems, loop, lemCounter;
static String allLems[];
public static void main(String[] args) {
//Ask for the number of lemurs
askNL();
//Initalize the length of the array to the total number of lemurs
allLems = new String[numLems];
//Ask which lemurs the user wants to generate
//Set the lemur counter and the lemur string to nothing
lemCounter = 0;
for(int i = 0; i < numLems; i++){
//Run the method that asks which lemur they want
askTL();
//Run the method to check which lemur the user wanted
checkTL();
//Use lemCounter to keep track of the lemur number
lemCounter++;
}
}
//Method asking how many lemurs, for the sake of cleaniness in the main method
static int askNL(){
do{
try{
String numLemsStr = JOptionPane.showInputDialog("How many Lemurs would you like to generate?");
numLems = Integer.parseInt(numLemsStr);
loop = 2;
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Not an acceptable input, please try again.");
loop = 1;
}
}while(loop==1);
return numLems;
}
//Method asking which type of Lemur
static int askTL(){
do{
try{
String typeLemsStr = JOptionPane.showInputDialog("What type of Lemur would you like for Lemur "+ lemCounter+1
+ "\n1 - Tree Lemur"
+ "\n2 - Desert Lemur"
+ "\n3 - Jungle Lemur");
typeLems = Integer.parseInt(typeLemsStr);
if(typeLems > 3){
JOptionPane.showMessageDialog(null, "Not an acceptable input, please try again.");
loop = 1;
}
else{
loop = 2;
}
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Not an acceptable input, please try again.");
loop = 1;
}
}while(loop==1);
return typeLems;
}
//Method to decide which lemur the user wanted
static String[] checkTL(){
if(typeLems==1){
//I'm not sure what I need to put in the name to get it to proceed linearly
TreeLemur L = new TreeLemur();
}
return allLems;
}