我正在做一个动物案例研究项目,在那里我制作了具有相同PIVS的不同继承类。我需要在格式正确的表格中打印代码。 我的代码:
public static void displayAll()
{
ArrayList <animals> animals= new ArrayList <animals>();
animals.add(new Tigers("Panthera tigris tigris","Asia", "The Bengal Tiger", "Tropical", 490, 7)); // Tigers
animals.add(new Tigers("Panthera tigris altaica","Asia", "The Siberian Tiger", "Woodlands", 400, 3));
animals.add(new Tigers("Panthera tigris sumatrae","Asia", "The Sumatran Tiger", "Tropical", 310, 5));
animals.add(new Tigers("Panthera tigris jacksoni","Asia", "The Malayan Tiger", "Tropical", 260, 5));
animals.add(new Tigers("Panthera tigris sondaica","Asia", "The Javan Tiger", "Tropical", 310, 2));
animals.add(new Tigers("Panthera tigris balica", "Asia", "The Bali Tiger", "Tropical", 200, 5));
animals.add(new Tigers("Panthera tigris virgata","Europe", "The Caspian Tiger", "Tropical", 530, 7));
animals.add(new Tigers("Panthera tigris amoyensis","Asia", "The South China Tiger", "Tropical", 330, 4));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Bulldog", "Temperate", 55, 2)); // Dogs
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Beagle", "Household", 24, 3));
animals.add(new Dogs("Canis lupus familiaris", "Asia", "The Pug", "Household", 20, 5));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Boxer", "Wild", 71, 4));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Poodle", "Household", 9, 2));
animals.add(new Dogs("Canis lupus familiaris", "Asia", "The Golden Retriever", "Tropical", 75, 3));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The German Shepherd", "Temperate", 88, 3));
animals.add(new Dogs("Canis lupus familiaris", "Europe", "The Rottweiler", "Wild", 130, 2));
animals.add(new Elephants("Elephant maximus", "Asia", "The Asian Elephant", "Tropical", 12000, 1)); // Elephants
animals.add(new Elephants("Elephant maximus indicus", "Asia", "The Indian Elephant", "Tropical", 12000, 12));
animals.add(new Elephants("Loxodonta africana", "Africa", "The African Bush Elephant", "Savvanah", 13000, 6));
animals.add(new Elephants("Loxodonta cyclotis", "Africa", "The African Forest Elephant", "Tropical", 60000, 2));
animals.add(new Elephants("Elephas maximus borneensis", "Asia", "The Borneo Elephant", "Tropical", 14000, 8));
animals.add(new Elephants("Elephas maximus maximus", "Asia", "The Sri Lankan Elephant", "Woodlands", 5500, 5));
animals.add(new Elephants("Palaeoloxodon antiquus", "Asia", "The Straight Tusked Elephant", "Temperate", 22600, 8));
animals.add(new Elephants("Palaeoloxodon cyrpriotes", "Asia", "The Cyprus Dwarf Elephant", "Tropical", 440, 3));
animals.add(new Seals("Mirounga", "North America", "The Elephant Seal", "Aquatic", 5000, 2)); // Seals
animals.add(new Seals("Phoca vitulina", "North America", "The Harbor Seal", "Aquatic", 130, 3));
animals.add(new Seals("Pagophilus groenlandicus", "North America", "The Harp Seal", "Artic", 290, 1));
animals.add(new Seals("Halichoerus grypus", "North America", "The Grey Seal", "Aquatic", 550, 3));
animals.add(new Seals("Pusa hispida", "Asia", "The Ringed Seal", "Artic", 160, 4));
animals.add(new Seals("Phoca largha", "North America", "The Spotted Seal", "Aquatic", 160, 3));
animals.add(new Seals("Hydrurga leptonyx", "Antarctica", "The Leopard Seal", "Artic", 780, 2));
animals.add(new Seals("Monachini", "North America", "The Monk Seal", "Aquatic", 600, 2));
animals.add(new Whales("Orcinus orca", "North America", "The Killer Whale", "Aquatic", 12000, 1)); // Whales
animals.add(new Whales("Delphinapterus", "North America", "The Beluga Whale", "Aquatic", 3000, 3));
animals.add(new Whales("Monodon monoceros", "Asia", "The Narwhal Whale", "Aquatic", 2000, 5));
animals.add(new Whales("Balaenoptera musculus", "Antarctica", "The Blue Whale", "Artic", 300000, 3));
animals.add(new Whales("Megaptera novaeangliae", "North America", "The Humpback Whale", "Aquatic", 66000, 5));
animals.add(new Whales("Livyatan melvillei", "North America", "The Livyatan Whale", "Aquatic", 100000, 2));
animals.add(new Whales("Eschrichtius robustus", "North America", "The Gray Whale", "Aquatic", 60000, 18));
animals.add(new Whales("Balaenoptera physalus", "Asia", "The Fin Whale", "Aquatic", 100000, 7));
}
我尝试过的事情:
我试图使用转义序列和for循环来打印我的代码。
System.out.println("Press Button To Display Animals");
scanner = new Scanner(System.in);
setScanner(new Scanner(scanner.nextLine()));
System.out.println(String.format("%-10s%-10s%-%10s-%10s-%10s-10s", " Scientific Name ", " Continent Of Orgin ", " Name, "Natural Habitat", "Weight", " Num Of Offspring"));
for (Animals a : animals)
System.out.println(String.format("%-10s%-10s%-10d", a.getscientificname(), a.getcontinentoforgin(), a.getname(), a.getnaturalhabitat(), a.getweight(), a.getnumofoffspring()));
它给了我很多错误
也只是为了获得奖金,我将如何建立一种搜索我的动物名单的方法,以找到所有亚洲本地的动物?
所有帮助将不胜感激
答案 0 :(得分:1)
public void displayAll(){
List<Animals> animals= new ArrayList();
animals.add(new Tiger("Panthera tigris tigris","Asia", "The Bengal Tiger", "Tropical", 490, 7));
animals.add(new Dog("Panthera tigris tigris","Asia", "The Bengal Tiger", "Tropical", 490, 7));
for(Animals a:animals){
System.out.println(String.format("name:%s,weight:%d",a.getName(),a.getWeight()));
}
}
打印:
name:Panthera tigris tigris,weight:490
name:Panthera tigris tigris,weight:490
答案 1 :(得分:0)
第一个明显的问题(错误会有所帮助)。
ArrayList <animals> animals= new ArrayList <animals>();
...
for (Animals a : animals)
类型名称已将大小写从animals
更改为Animals
。