java - 程序不打印索引 0 处的元素
问题描述
我正在处理案例 3,我需要打印有关 Airplane 类和 BatMobile 类的 3 个实例的一些详细信息。该循环仅打印出索引 1 和 2 处的元素以及 BatMobile 类的其他 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" );
}
}
推荐阅读
- reactjs - 超时 - 在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调
- javascript - 当并非所有字段都完成时,如何创建将确认提交表单的 JS 函数
- javascript - 获取后返回一个值
- wordpress - 编辑预制的 wordpress 主题时未应用某些 CSS 样式
- python - 下载数据集,这是一个 Zip 文件,在笔记本中包含大量 csv 文件以进行数据分析
- node.js - 如何安装所有相关软件包?
- php - Laravel 5.1 中的文章、评论和用户关系
- android - 如何预览我的自适应图标?
- wordpress - 为什么我的帖子在分类菜单中格式不正确,但帖子预览很好?
- python - 将文件读入列表分隔值