我正在尝试编写一个接受用户输入并将其存储在数组中的程序。它非常复杂,我环顾了一下并阅读了本书。我只是无法弄清楚如何做到这一点。这是我的提示
修改实验室20,将拷贝构造函数添加到petRecord,catRecord,dogRecord和birdRecord。使用HW 6中的主文件作为main的起点。当用户填写每只宠物的信息时,还包括让他们通过在键盘上输入1,2或3来选择猫,狗或鸟。将适当的对象存储到每个数组槽(catRecord,dogRecord或birdRecord)中。当比较看两只宠物是否相同时,一定要检查它们是否是相同类型的动物以及匹配数据,并确保numFeathers与鸟类匹配,并且hasLongHair匹配猫/狗。
除了数组之外,还要创建一个额外的宠物,其中包含用户提供的信息(对于数组中的五个),然后使用其“复制构造函数”将其复制到新对象。通过显示它们是相同的来测试这是否有效(就像测试阵列中的任何宠物是否相同时一样)。
主要是我无法计算动物的所有重量和年龄。我不知道该怎么做,只需要一些方向。
public class petRecordMain
{
public static void main(String[] args)
{ Scanner in = new Scanner(System.in);
PetRecord[] petArray = new PetRecord[6];
DogRecord[] dogArray = new DogRecord[6];
CatRecord[] catArray = new CatRecord[6];
BirdRecord[] birdArray = new BirdRecord[6];
double averageWeight = 0;
double averageAge = 0;
int whichPet = 0;
for(int i = 0; i < petArray.length; i++)
{
petArray[i] = new PetRecord();
dogArray[i] = new DogRecord();
catArray[i] = new CatRecord();
birdArray[i] = new BirdRecord();
}
for(int i = 0; i < 6; i++)
{
System.out.println("Please choose, 1 for Cat, 2 for Dog, 3 for Bird: ");
whichPet = in.nextInt();
switch(whichPet)
{
case 1:
System.out.println("Cat");
System.out.println("Enter the name of Pet "+ i + " : ");//Asks user for input
catArray[i].setName(in.next()); //For five animals, weight, age
System.out.println("Enter the weight of Pet "+ i + " : "); //add up then divide by all five weight
catArray[i].setWeight(in.nextInt());
System.out.println("Enter the age of Pet"+ i + " : ");
catArray[i].setAge(in.nextInt());
break;
case 2:
System.out.println("Dog");
System.out.println("Enter the name of Pet "+ i + " : ");//Asks user for input
dogArray[i].setName(in.next()); //For five animals, weight, age
System.out.println("Enter the weight of Pet "+ i + " : "); //add up then divide by all five weight
dogArray[i].setWeight(in.nextInt());
System.out.println("Enter the age of Pet"+ i + " : ");
dogArray[i].setAge(in.nextInt());
break;
case 3:
System.out.println("Bird");
System.out.println("Enter the name of Pet "+ i + " : ");//Asks user for input
birdArray[i].setName(in.next()); //For five animals, weight, age
System.out.println("Enter the weight of Pet "+ i + " : "); //add up then divide by all five weight
birdArray[i].setWeight(in.nextInt());
System.out.println("Enter the age of Pet"+ i + " : ");
birdArray[i].setAge(in.nextInt());
break;
default :
System.out.println("Invalid input, try again.");
}
}
在代码下面有点乱。我有一个for
循环,我认为我可以用来计算所有的重量和年龄,但这就是我现在卡住的地方。
for(int i = 0; i < 6; i++)
{
averageWeight += petArray.getWeight[i]; //How would I calculate all weight?
//How would I caculate all ages?
System.out.println("");
}
/*
averageWeight = (petOne.getWeight()+petTwo.getWeight()+petThree.getWeight()+petFour.getWeight()+petFive.getWeight()) /5;
averageAge = (petOne.getAge()+petTwo.getAge()+petThree.getAge()+petFour.getAge()+petFive.getAge()) /5;
System.out.println("The average weight of all dogs is: "+averageWeight+" Pounds");
System.out.println("The average age of all dogs is: "+averageAge+" years old");
System.out.println("");
if(petOne.getName().equalsIgnoreCase(petTwo.getName()) && petThree.getName().equalsIgnoreCase(petFour.getName()) && petFour.getName().equalsIgnoreCase(petFive.getName()) )
{
System.out.println("All five dogs have the same name.");
}
else if(petOne.getWeight() == petTwo.getWeight() && petThree.getWeight() == petFour.getWeight() && petFour.getName() == petFive.getName())
{
System.out.println("All five dogs have the same weight");
}
else if(petOne.getAge() == petTwo.getAge() && petThree.getAge() == petFour.getAge() && petFour.getAge() == petFive.getAge())
{
System.out.println("All five dogs are the same age.");
}
//Below check if any pets are the same
//1
if(petOne.toString().equals(petTwo.toString()))
{
System.out.println(petOne.toString()+" and "+petTwo.toString()+" are the same");
}
else if(petOne.toString().equals(petThree.toString()))
{
System.out.println(petOne.toString()+" and "+petThree.toString()+" are the same");
}
else if(petOne.toString().equals(petFour.toString()))
{
System.out.println(petOne.toString()+" and "+petFour.toString()+ "are the same");
}
else if(petOne.toString().equals(petFive.toString()))
{
System.out.println(petOne.toString()+" and "+petFive.toString()+" are the same");
}
//2
if(petTwo.toString().equals(petOne.toString()))
{
System.out.println(petTwo.toString()+" and "+petOne.toString()+" are the sae");
}
else if(petTwo.toString().equals(petThree.toString()))
{
System.out.println(petTwo.toString()+" and "+petThree.toString()+" are the same");
}
else if(petTwo.toString().equals(petFour.toString()))
{
System.out.println(petTwo.toString()+" and "+petFour.toString()+" are the same");
}
else if(petTwo.toString().equals(petFive.toString()))
{
System.out.println(petTwo.toString()+" and "+petFive.toString()+" are the same");
}
//3
if(petThree.toString().equals(petOne.toString()))
{
System.out.println(petThree.toString()+" and "+petOne.toString()+" are the same");
}
else if(petThree.toString().equals(petTwo.toString()))
{
System.out.println(petThree.toString()+" and "+petTwo.toString()+" are the same");
}
else if(petThree.toString().equals(petFour.toString()))
{
System.out.println(petThree.toString()+" and "+petFour.toString()+" are the same");
}
else if(petThree.toString().equals(petFive.toString()))
{
System.out.println(petThree.toString()+" and "+petFive.toString()+" are the same");
}
//4
if(petFour.toString().equals(petOne.toString()))
{
System.out.println(petFour.toString()+" and "+petOne.toString()+" are the same");
}
else if(petFour.toString().equals(petTwo.toString()))
{
System.out.println(petFour.toString()+" and "+petTwo.toString()+" are the same");
}
else if(petFour.toString().equals(petThree.toString()))
{
System.out.println(petFour.toString()+" and "+petThree.toString()+" are the same");
}
else if(petFour.toString().equals(petFive.toString()))
{
System.out.println(petFour.toString()+" and "+petFour.toString()+" are the same");
}
//5
if(petFive.toString().equals(petOne.toString()))
{
System.out.println(petFive.toString()+" and "+petOne.toString()+" are the same");
}
else if(petFive.toString().equals(petTwo.toString()))
{
System.out.println(petFive.toString()+" and "+petTwo.toString()+" are the same");
}
else if(petFive.toString().equals(petThree.toString()))
{
System.out.println(petFive.toString()+" and "+petThree.toString()+" are the same");
}
else if(petFive.toString().equals(petFour.toString()))
{
System.out.println(petFive.toString()+" and "+petFour.toString()+" are the same");
}
//Below caculates which pet is the youngest
if(petOne.getAge() < petTwo.getAge() && petOne.getAge() < petThree.getAge() && petOne.getAge() < petFour.getAge() && petOne.getAge() < petFive.getAge())
{
System.out.println(petOne.getName()+" is the youngest pet.");
}
else if(petTwo.getAge() < petOne.getAge() && petTwo.getAge() < petThree.getAge() && petTwo.getAge() < petFour.getAge() && petTwo.getAge() < petFive.getAge())
{
System.out.println(petTwo.getName()+" is the youngest pet.");
}
else if(petThree.getAge() < petOne.getAge() && petThree.getAge() < petTwo.getAge() && petThree.getAge() < petFour.getAge() && petThree.getAge() < petFive.getAge())
{
System.out.println(petThree.getName()+" is the youngest pet.");
}
else if(petFour.getAge() < petOne.getAge() && petFour.getAge() < petTwo.getAge() && petFour.getAge() < petThree.getAge() && petFour.getAge() < petFive.getAge())
{
System.out.println(petFour.getName()+" is the youngest pet.");
}
else if(petFive.getAge() < petOne.getAge() && petFive.getAge() < petTwo.getAge() && petFive.getAge() < petThree.getAge() && petFive.getAge() < petFour.getAge())
{
System.out.println(petFive.getName()+" is the youngest pet.");
}
//Below caculates which pet is the oldest
if(petOne.getAge() > petTwo.getAge() && petOne.getAge() > petThree.getAge() && petOne.getAge() > petFour.getAge() && petOne.getAge() > petFive.getAge())
{
System.out.println(petOne.getName()+" is the oldest pet.");
}
else if(petTwo.getAge() > petOne.getAge() && petTwo.getAge() > petThree.getAge() && petTwo.getAge() > petFour.getAge() && petTwo.getAge() > petFive.getAge())
{
System.out.println(petTwo.getName()+" is the oldest pet.");
}
else if(petThree.getAge() > petOne.getAge() && petThree.getAge() > petTwo.getAge() && petThree.getAge() > petFour.getAge() && petThree.getAge() > petFive.getAge())
{
System.out.println(petThree.getName()+" is the oldest pet.");
}
else if(petFour.getAge() > petOne.getAge() && petFour.getAge() > petTwo.getAge() && petFour.getAge() > petThree.getAge() && petFour.getAge() > petFive.getAge())
{
System.out.println(petFour.getName()+" is the oldest pet.");
}
else if(petFive.getAge() > petOne.getAge() && petFive.getAge() > petTwo.getAge() && petFive.getAge() > petThree.getAge() && petFive.getAge() > petFour.getAge())
{
System.out.println(petFive.getName()+" is the oldest pet.");
}
//Below caculates which pet is the smallest
if(petOne.getWeight() < petTwo.getWeight() && petOne.getWeight() < petThree.getWeight() && petOne.getWeight() < petFour.getWeight() && petOne.getWeight() < petFive.getWeight())
{
System.out.println(petOne.getName()+" is the smallest pet.");
}
else if(petTwo.getWeight() < petOne.getWeight() && petTwo.getWeight() < petThree.getWeight() && petTwo.getWeight() < petFour.getWeight() && petTwo.getWeight() < petFive.getWeight())
{
System.out.println(petTwo.getName()+" is the smallest pet.");
}
else if(petThree.getWeight() < petOne.getWeight() && petThree.getWeight() < petTwo.getWeight() && petThree.getWeight() < petFour.getWeight() && petThree.getAge() < petFive.getWeight())
{
System.out.println(petThree.getName()+" is the smallest pet.");
}
else if(petFour.getWeight() < petOne.getWeight() && petFour.getWeight() < petTwo.getWeight() && petFour.getWeight() < petThree.getWeight() && petFour.getWeight() < petFive.getWeight())
{
System.out.println(petFour.getName()+" is the smallest pet.");
}
else if(petFive.getWeight() < petOne.getWeight() && petFive.getWeight() < petTwo.getWeight() && petFive.getWeight() < petThree.getWeight() && petFive.getWeight() < petFour.getWeight())
{
System.out.println(petFive.getName()+" is the smallest pet.");
}
//Below caculates to see which pet is the largest
if(petOne.getWeight() > petTwo.getWeight() && petOne.getWeight() > petThree.getWeight() && petOne.getWeight() > petFour.getWeight() && petOne.getWeight() > petFive.getWeight())
{
System.out.println(petOne.getName()+" is the largest pet.");
}
else if(petTwo.getWeight() > petOne.getWeight() && petTwo.getWeight() > petThree.getWeight() && petTwo.getWeight() > petFour.getWeight() && petTwo.getWeight() > petFive.getWeight())
{
System.out.println(petTwo.getName()+" is the largest pet.");
}
else if(petThree.getWeight() > petOne.getWeight() && petThree.getWeight() > petTwo.getWeight() && petThree.getWeight() > petFour.getWeight() && petThree.getAge() > petFive.getWeight())
{
System.out.println(petThree.getName()+" is the largest pet.");
}
else if(petFour.getWeight() > petOne.getWeight() && petFour.getWeight() > petTwo.getWeight() && petFour.getWeight() > petThree.getWeight() && petFour.getWeight() > petFive.getWeight())
{
System.out.println(petFour.getName()+" is the largest pet.");
}
else if(petFive.getWeight() > petOne.getWeight() && petFive.getWeight() > petTwo.getWeight() && petFive.getWeight() > petThree.getWeight() && petFive.getWeight() > petFour.getWeight())
{
System.out.println(petFive.getName()+" is the largest pet.");
}
*/
答案 0 :(得分:1)
我相信CatRecord必须继承PetRecord,在回答之前会在评论中澄清这一点但不幸的是,StackOverflow不允许我发表评论。如果重量存储在PetRecord中,因为所有宠物都有重量而不管物种,那将是有道理的。这样的事情应该有效:
averageWeight += petArray[i].getWeight();
同样在初始化时,请在以下行中编写代码:
petarray[i] = new CatRecord();