我有一个类“车辆”,它应该允许使用用户输入的信息保存数组,但是当我尝试使用计数器接受数据时,每个数据条目都有一个唯一的数字它不接受任何超过[on vehicle vehiclearray[totalcount] = new vehiclearray(count,licence,date,value,colour);
我相对较新的java并且这个问题困扰了我,任何帮助都会受到赞赏。
这是整个代码,问题在submenu1下。
import java.util.Scanner;
class main
{
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
int option = menu();
int totalcount = submenu1.totalcount;
int carcount = submenu1.carcount;
int bikecount = submenu1.bikecount;
vehicle vehiclearray[] = new vehiclearray[100];
if (option == 1)
submenu1(vehiclearray);
else if (option ==2)
submenu2(totalcount, vehiclearray);
else if (option ==3)
submenu3(totalcount, carcount, bikecount,museumbike, museumcar);
else if (option ==4)
submenu4(carcount, museumcar);
else if (option ==5)
submenu5(bikecount, museumbike);
else if (option ==6)
System.out.println("Thank you for using this program");
else
System.out.println("Invalid selection, please try again.");
}
}
class menusystem
{
public static int menu()
{
Scanner input = new Scanner (System.in);
int option = 0;
System.out.println("Please select an option");
System.out.println("=======================");
System.out.println("1. Add a vehicle to the collection");
System.out.println("2. List vehices manufactured by a specifit year");
System.out.println("3. List all vehcles of a certain engine size or more");
System.out.println("4. list all cars");
System.out.println("5. List all bikes");
System.out.println("6. Exit");
option = input.nextInt();
return option;
}
public static int submenu1(vehicle vehiclearray[])
{
Scanner input = new Scanner (System.in);
String suboption1;
int totalcount = carcount = bikecount = 1;
System.out.println("Is the vehicle a car or a bike? (exit to close)");
suboption1 = input.next();
while(!suboption1.equalsIgnoreCase("exit"))
{
if (suboption1.equalsIgnoreCase("car"))
{
vehicle.id=count;
System.out.println("Please enter the licence, manufacture year, car value and colour of the car");
vehicle vehiclearray[totalcount] = new vehiclearray(count,licence,date,value,colour);
System.out.println("Please enter the number of doors, number of seats, engine type (petrol or deisol) and engine size in litres");
museumcar museumcar[carcount] = new museumcar(cardoors,seats,enginetype,carenginesize);
totalcount++;
carcount++;
}
else if (suboption1.equalsIgnoreCase("bike"))
{
System.out.println("Please enter the licence, manufacture year, car value and colour of the bike");
vehicle vehiclearray[totalcount] = new vehiclearray(licence,date,value,colour);
vehicle.id=count;
System.out.println("Please enter the bike type (sports, tourer or trials) and engine size in cc");
museumbike museumbike[bikecount] = new museumbike(type,bikeenginesize);
totalcount++;
bikecount++;
}
else
System.out.println("Please enter a valid entry.");
System.out.println("Is the vehicle a car or a bike? (exit to close)");
suboption1 = input.next();
return bikecount;
return carcount;
return totalcount;
}
}
public static void submenu2(int totalcount, vehicle vehiclearray[])
{
Scanner input = new Scanner (System.in);
int desiredyear = 0;
int count = 1;
System.out.println("What vehicle manufacture date do you want to see?");
desiredyear = input.nextInt();
for (count = 1; count <= totalcount; count++)
{
if (vehiclearray[totalcount].getdate == desiredyear)
System.out.println(vehiclearray[totalcount] +" id : "+ vehiclearray[totalcount].getlicence(), +" licence : "+ vehiclearray[totalcount].getdate(), +" date : "+ vehiclearray[totalcount].getvalue(), +" value : "+ vehiclearray[totalcount].getcolour() +" colour:");
else
System.out.println(vehiclearray[totalcount] +" does not match the desired manufacture year");
}
}
public static void submenu3(int totalcount, int carcount, int bikecount, museumbike museumbike[], museumcar museumcar[])
{
Scanner input = new Scanner (System.in);
double desiredsize = 0;
int count = 1;
System.out.println("What vehicle size or more do you want to see?");
desiredsize = input.nextDouble();
for (count = 1; count <= totalcount; count++)
{
if (desiredsize >=0 && desiredsize <=100)
{
if (museumcar[carcount].getcarenginesize >= desiredsize)
System.out.println(museumcar[carcount].getcardoors(), +(" doors : ")+ museumcar[carcount].getseats(), +(" seats : ")+ museumcar[carcount].getenginetype(), +(" : ")+ museumcar[carcount].getcarenginesize() +("ltrs"));
else
System.out.println("This car has a lower engine size then the desiredsize");
}
else if (desiredsize >=100)
System.out.println(museumbike[bikecount].gettype(), +(" : ")+ museumbike[bikecount].getbikeenginesize() +("cc"));
}
}
public static void submenu4(int carcount, museumcar museumcar[])
{
int count = 1;
for (count = 1; count <= museumcar[carcount]; count++)
{
System.out.println(museumcar[carcount].getcardoors(), +(" doors : ")+ museumcar[carcount].getseats(), +(" seats : ")+ museumcar[carcount].getenginetype(), +(" : ")+ museumcar[carcount].getcarenginesize() +("ltrs"));
}
}
public static void submenu5(int bikecount, museumbike museumbike[])
{
int count = 1;
for (count = 1; count <= museumbike[bikecount]; count++)
{
System.out.println(("bike id:")+ museumbike[bikecount].gettype(), +(" : ")+ museumbike[bikecount].getbikeenginesize() +("cc"));
}
}
}
class vehicle
{
private int carid;
private String licence;
private int date;
private Double value;
private String colour;
vehicle (int i,String l, int d, double v, String c)
{
id = i;
licence = l;
date = d;
value = v;
colour = c;
}
int getid()
{
return id;
}
String getlicence()
{
return licence;
}
int getdate()
{
return date;
}
Double getvalue()
{
return value;
}
String getcolour()
{
return colour;
}
}
class museumcar extends vehicle
{
private int cardoors;
private int seats;
private String enginetype;
private Double carenginesize;
museumcar (int cd, int s, String et, Double ces)
{
super (i, l, d, v, c);
cardoors = cd;
seats = s;
enginetype = et;
carenginesize = es;
}
int getcardoors()
{
return cardoors;
}
int getseats()
{
return seats;
}
String getenginetype()
{
return enginetype;
}
Double getcarenginesize()
{
return carenginesize;
}
}
class museumbike extends vehicle
{
private String type;
private Double bikeenginesize;
museumbike (String t, Double bes)
{
super (i, l, d, v, c);
type = t;
bikeenginesize = bes;
}
String gettype()
{
return type;
}
Double getbikeenginesize()
{
return bikeenginesize;
}
}
答案 0 :(得分:0)
没有cararray课程,所以你不能new vehiclearray[...]
,而应该new vehicle[...]
。
例如,这行代码不合法:
vehicle vehiclearray[] = new vehiclearray[100];
但这是:
vehicle vehiclearray[] = new vehicle[100];
一般来说,我认为大多数声明我们的变量与类上的数组括号而不是变量:
vehicle[] vehiclearray = new vehicle[100];
虽然你的方式是合法的(如果你创建了正确的数组对象声明)。
附加说明: