Java 我一直在获取数组的最后一个输入
因此,用户必须输入一个类似“Select 1”的代码,然后输入“Options”。这些选项应该在最上面给出ArrayList的输出,而不是在最下面给出ArrayList的输出。我被要求使用4类以及。提前谢谢你!如果需要,我还可以提供更多详细信息!:] 编辑:当用户输入“选择1”时,应给出用户选择的汽车。当他在之后输入“选项”时,它应该为此人提供3个特定汽车的选项。除此之外,它只为我的数组中的选项提供最后一个输入Java 我一直在获取数组的最后一个输入,java,arrays,list,Java,Arrays,List,因此,用户必须输入一个类似“Select 1”的代码,然后输入“Options”。这些选项应该在最上面给出ArrayList的输出,而不是在最下面给出ArrayList的输出。我被要求使用4类以及。提前谢谢你!如果需要,我还可以提供更多详细信息!:] 编辑:当用户输入“选择1”时,应给出用户选择的汽车。当他在之后输入“选项”时,它应该为此人提供3个特定汽车的选项。除此之外,它只为我的数组中的选项提供最后一个输入 public static void main(String[] args) {
public static void main(String[] args)
{
Dealership dealership = new Dealership();
System.out.println("To view a list of commands, enter Commands");
Scanner sc = new Scanner(System.in);
while(true)
{
String input = sc.nextLine();
String[] parts = input.split(" ");
switch(parts[0])
{
case "Commands":
System.out.println("Commands: shows a list of all available commands\n" +
"Select [n]: selects car No. n and shows the details\n" +
"Options: show the details of options installed on the selected car\n" +
break;
case "Options":
System.out.println(dealership.ShowOptions());
break;
}
经销商。等级
{
private Car[] cars;
private Car selectedCar;
public Dealership() // Resonsibility: Hold the inventory of cars.
{
cars = new Car[10]; // 3 means there is 3 cars that goes from 0, 1, 2 which also is car 1, 2, 3
Engine engine = new Engine(FuelType.Gas, 4, 67001, 162, 24); //FuelType, int noOfCylinders, int capacity, int horsePower, float mpg
Interior interior = new Interior("Brown", "Maroon", false, false); //(String color1, String color2, boolean hasSunRoof, boolean hasMoonRoof)
Trunk trunk = new Trunk(true, false, true, true, "White"); // (boolean hasSpareTire, boolean hasFirstAidKit, boolean hasCarpet, boolean hasJack, String carpetColor)
Car car = new Car("Hyundai", 2006, "Sonata", 2500, "White", CarType.Sedan, engine, interior, trunk); // Parameter in the Public Car.Class section
cars[0] = car; //
engine = new Engine(FuelType.Gas, 4, 75708, 325, 17);
interior = new Interior("Black", "Blue", true, false);
trunk = new Trunk(true, false, true, false, "Black");
car = new Car("Infiniti", 2016, "QX50", 38000, "Black", CarType.Sedan, engine, interior, trunk);
cars[1] = car;
engine = new Engine(FuelType.Gas, 4, 49967, 132, 26);
interior = new Interior("White", "Beige", true, true);
trunk = new Trunk(true, false, true, true, "Brown");
car = new Car("Toyota", 2010, "Corolla", 7845, "Red", CarType.Sedan, engine, interior, trunk);
cars[2] = car;
public String ListAllCars() //Responsibility: Returns a description of all cars
{
String result = "";
for(int i = 0; i < cars.length; i++)
{
Car c = cars[i];
String s = c.toString();
result = result + (i+1) + "-" + s + "\n";
}
return result;
}
public String SelectCar (int index)
{
selectedCar = cars[index - 1];
return selectedCar.toString();
}
public String ShowOptions() // Responsibility: Returns the options installed on the selected car
{
if(selectedCar != null)
return selectedCar.ShowOptions();
else
return "Please select a car first";
}
}
选项。类
{
private String name;
private String name2;
private String name3;
private String description;
private String description2;
private String description3;
public Option (String name, String description, String name2, String description2, String name3, String description3)
{
this.name = name;
this.name2 = name2;
this.name3 = name3;
this.description = description;
this.description2 = description2;
this.description3 = description3;
}
public String toString() //Responsibility: Returns a description of the option(Name, Description)
{
String options = "The first option for this car has " + name + description + ", the second option for this car has" + name2 + description2 + ","
+ " and the third option for this car has" + name3 + description3 + ".";
return options;
}
}
在Car类中,您应该返回options.toString(),您的经销商类应该返回一个字符串数组。显示您现在拥有的内容和期望的内容。您的问题很难理解,因此请添加更多细节
main()
毫无意义。什么是selectedCar
ShowOptions
仅返回option
最后一个值的字符串,而不是您以前放入数组的所有值。对不起,没有问题,但如何返回字符串数组?我的脑子有点紧张。返回类型应该是String[]
{
private String name;
private String name2;
private String name3;
private String description;
private String description2;
private String description3;
public Option (String name, String description, String name2, String description2, String name3, String description3)
{
this.name = name;
this.name2 = name2;
this.name3 = name3;
this.description = description;
this.description2 = description2;
this.description3 = description3;
}
public String toString() //Responsibility: Returns a description of the option(Name, Description)
{
String options = "The first option for this car has " + name + description + ", the second option for this car has" + name2 + description2 + ","
+ " and the third option for this car has" + name3 + description3 + ".";
return options;
}
}