java - 使用静态变量查找数组中的元素数
问题描述
我正在开发一个车辆信息系统的项目。我已将所有实例存储在一个数组中,并且必须使用静态变量打印出其中的元素/实例的数量。我尝试了下面的代码,但我猜我一直得到 0,因为它引用了类中定义的计数(初始化为 0),而不是构造函数中具有实例总数的计数。我怎样才能解决这个问题?
package edu.uga.cs1302.vehicles;
import java.util.Scanner;
public class vehicleInstancesAndMenu {
static int count;
public vehicleInstancesAndMenu() {
menu();
}//constructor
public void vehicles() {
Vehicle[] vehicles = new Vehicle[15];
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);
for(int i = 0; i < vehicles.length; i++) {
count = vehicles.length;
//System.out.println(vehicles[i] );
}//for
System.out.println(count);
}//vehicles
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 for your system.\nq - To terminate the program.");
response = scan.nextLine();
switch(response) {
case "1":
if(response.equals("1")) {
System.out.println(vehicleInstancesAndMenu.count);
}
}
}
}//vehicleInstancesAndMenu
解决方案
您应该能够通过在函数中包含vehicle();
在您的行menu()
之前的某个位置来解决此问题。System.out.println('..');
目前该函数从未被调用,因此它没有计算任何东西。而不是在函数中使用循环vehicle()
,只需count = vehicles.length;
按照注释中的说明进行设置。
推荐阅读
- django - Django寻找模板文件的错误路径
- ios - (点击)事件仅适用于 IOS 设备 - Angular 10 应用程序
- java - 基于 Flutter 的 Android 应用程序抛出 MissingPluginException(未找到通道上的方法的实现)
- java - 调用 onSaveInstanceState 后 GridLayoutManager 变为 LinearLayoutManager
- java - 如何使用android studio在firebase上注册表格包括fname姓氏等
- python - 如何仅在 python 中使用输入函数在列表中输入值?
- javascript - SVG在滚动动画上调整大小断断续续/口吃
- java - 我无法在 Eclipse 中连接数据库
- java - 如何使用 Selenium 和 Java 将文本发送到 Angular 页面中的金额字段
- python-3.x - 调试 Numpy VisibleDeprecationWarning(来自参差不齐的嵌套序列的 ndarray)