[英]Program not printing element at index 0
我正在處理案例 3,我需要打印有關飛機 class 和 BatMobile class 的 3 個實例的一些詳細信息。 該循環僅打印出索引 1 和 2 處的元素以及 BatMobile class 的其他 3 個實例。 為什么會發生這種情況,如何打印兩個類的所有 3 個實例?
import java.util.Scanner;
public class vehicleInstancesAndMenu {
static int count;
Vehicle[] vehicles = new Vehicle[15];
public vehicleInstancesAndMenu() {
menu();
}//constructor
public void vehicles_Instances() {
vehicles[0] = new Airplane("Airbus A220-100", "Delta Airlines", "2019", 2, 120, 300, 221, 250);
vehicles[1] = new Airplane("Airbus A319-100", "Delta Airlines", "2018", 2, 200, 450, 319, 300);
vehicles[2] = new Airplane("Airbus A320-200", "Delta Airlines", "2018", 2, 220, 500, 320, 320);
vehicles[3] = new Automobile("Corolla LE", "Toyota", "2020", 300, 6, 150);
vehicles[4] = new Automobile("Civic SE", "Honda", "2018", 310, 6, 160);
vehicles[5] = new Automobile("Sentra", "Nissan", "2019", 360, 6, 200);
vehicles[6] = new Ship("APM - Maersk", "Maersk", "1904", 400000, "Carnival",2695, 35);
vehicles[7] = new Ship("MSC", "Mediterranean Shipping Company", "1970", 30000 ,"Mediterranean Shipping Company",2565, 32);
vehicles[8] = new Ship("COSCO", "China Ocean Shipping Company", "1990", 650000, "China Ocean Shipping Company",36201, 40);
vehicles[9] = new Tesla("Tesla Model 3", "Tesla", "2020", 1, 250, 200, 6, 300);
vehicles[10] = new Tesla("Tesla Model S", "Tesla", "2020", 1, 300, 220, 6, 390);
vehicles[11] = new Tesla("Tesla Model Y", "Tesla", "2020", 1, 400, 360, 6, 420);
vehicles[12] = new BatMobile("Model A","Wayne Industries", "2020", 4 , 300, 600 , 40000, "Wayne Industries", 500, 320);
vehicles[13] = new BatMobile("Model C", "Wayne Industries", "2019", 3 , 290, 400, 32000, "Wayne Industries", 450, 310);
vehicles[14] = new BatMobile("Model T", "Wayne Industries", "2018",3 , 250, 360, 280000, "Wayne Industries", 370, 310);
count = vehicles.length;
}
public void menu() {
Scanner scan = new Scanner(System.in);
String response;
System.out.println("Welcome! \n1 - To see how many vehicles are in the system\n2 - To see the name and the class of each vehicle\n3 - To see which vehicles can fly.\n4 - To see which vehicles can float.\n5 - To see which vehicles can fly and float.\n6 - To see a description of each vehicle.\nh - to see brief help information about the system.\nq - To terminate the program.");
response = scan.nextLine();
switch(response) {
case "1":
if(response.equals("1")) {
vehicles_Instances();
System.out.println("There are "+vehicleInstancesAndMenu.count+" vehicles in the system.");
}
case "2":
if(response.equals("2")) {
for(int i = 0; i< vehicles.length;i++) {
vehicles_Instances();
System.out.println( "Name: "+vehicles[i].getName()+" Class: "+vehicles[i].getClass().getSimpleName()+"\n" );
}
}
case "3":
if(response.equals("3")) {
Vehicle airplane = new Airplane(response, response, response, 0, 0, 0, 0, 0);
Vehicle batMobile = new BatMobile(response, response, response, 0, 0, 0, 0, response, 0, 0);
for(int i = 0; i < vehicles.length; i++) {
if( (vehicles[i] instanceof Airplane) || (vehicles[i] instanceof BatMobile) ){
System.out.println( "Name: "+vehicles[i].getName() + "\tType: "+vehicles[i].getClass().getSimpleName() );
System.out.println();
}else {
}
vehicles_Instances();
}
}
第三種情況:
case "3":
if(response.equals("3")) {
vehicles_Instances(); // you probably want to populate the vehicles first
Vehicle airplane = new Airplane(response, response, response, 0, 0, 0, 0, 0);
Vehicle batMobile = new BatMobile(response, response, response, 0, 0, 0, 0, response, 0, 0);
// the two above are not being used currently
/*
you can potentially do:
vehicles[0] = airplane;
vehicles[1] = batMobile;
*/
for(int i = 0; i < vehicles.length; i++) {
if( (vehicles[i] instanceof Airplane) || (vehicles[i] instanceof BatMobile) ){
System.out.println( "Name: "+vehicles[i].getName() + "\tType: "+vehicles[i].getClass().getSimpleName() );
System.out.println();
}else {
}
}
}
同樣在第二種情況下,您不想在每次循環迭代時初始化車輛:
case "2":
if(response.equals("2")) {
vehicles_Instances(); // should be here, not inside the loop
for(int i = 0; i< vehicles.length;i++) {
System.out.println( "Name: "+vehicles[i].getName()+" Class: "+vehicles[i].getClass().getSimpleName()+"\n" );
}
}
由於數組在第一次運行時使用 null 值初始化
`if( (vehicles[i] instanceof Airplane) || (vehicles[i] instanceof BatMobile) )`
它返回 false 並運行vehicles_Instances()
填充數組,但隨后 for 循環遞增,因此它將打印索引 1。
所以只需運行vehicles_Instances();
在 for 循環之前
這是因為您在 for 循環的第一次迭代之后調用了 vehicle_Instances()! 您的循環不會遍歷第一個元素,因為尚未創建列表! 在 for 循環執行之前實例化 vehicle_Instances() !
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.