-1

这是我的课

package javaapplication1;

public class Car {

private String model;
private String reg;
private String colour;
private double litreEngine;

//Default Constructor
public Car() {
    model = null;
    reg = null;
    colour = null;
    litreEngine = -1;
}

public Car(String startModel, String startReg, String startColour, double startLitreEngine) {
    model = startModel;
    reg = startReg;
    colour = startColour;
    litreEngine = startLitreEngine;
}




//Prints out Car attributes

public void print() {
    System.out.println("Model:" + model + "Reg:" + reg + "Colour:" + colour + "litreEngine:" + litreEngine);
}

public String toString() {
   return ("Model:" + model + " Reg: " + reg + " Colour: " + colour + " Litre Engine: " + litreEngine);
}

//Get and set Methods
public String getModel() {
    return model;
}

public String getReg() {
    return reg;
}

public String getColour() {
    return colour;
}

public double getLitreEngine() {
    return litreEngine;
}

public void setModel(String new_model)
{
    model = new_model;
}

public void setReg(String new_Reg)
    {
        reg = new_Reg;
}

public void setnumberOfHoursPerWeek(String new_colour)
{
            colour = new_colour;
}

public void setcompulsory(double new_litreEngine)
{
            litreEngine = new_litreEngine;
}

}

这是我的测试课

package javaapplication1;

import java.util.Scanner;

public class CarTestApp {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Car[] values = new Car[4];
    int i = 0;
    String Continue = "Y";
    String Stop = "N";
    String answer = null;

    //Car c = new Car("VW Golf", "01D1234", "Red", 1.6);
    //Car d = new Car("Mazda MX-5", "03WW324", "Olive", 1.6);
    //Car e = new Car();

    // Creates car details from user input.
    do {
        System.out.println("Enter model details: ");
        String model = in.nextLine();

        System.out.println("Enter reg details: ");
        String reg = in.nextLine();

        System.out.println("Enter colour details: ");
        String colour = in.nextLine();

        System.out.println("Enter engine details:(Number) ");
        String line = in.nextLine();
        double litreEngine = Double.parseDouble(line);
        //double litreEngine = in.nextDouble();

        Car one = new Car(model, reg, colour, litreEngine);
        values[i] = one;
        i++;

        if (i < values.length) {
            System.out.println("Do you want to add another car?(Y/N)");
            answer = in.nextLine();
        }
    } while (answer.equalsIgnoreCase(Continue) && i < values.length);



    System.out.println("Thank you for inputting this information");

    System.out.println(values[0]+"\t"+values[1]+"\t"+values[2]+"\t"+values[3]);
}
}
4

1 回答 1

0

我认为您需要一个菜单​​驱动的程序来与用户交互以执行不同的任务。

首先,您需要不同的方法,这将完成添加汽车的工作。然后你可以有一个开关盒,它可以处理你的各种情况。

public static void main(String args) {

    int choice = 0;
    char redo = 'n';

    do {   
        System.out.println("Enter your choice:");
        System.out.println("1. Add a car \n 2. Exit");

        choice = /** Read choice from user **/

        switch (choice) {
              case 1: addCar();                          
                      System.out.println("Do you want to continue? (Y or N)?");
                      redo = /** read char from user ('y' or 'n') **/
                      break;

              case 2: redo = 'n';
                      break;
        }

    } while (redo == 'Y' || redo == 'y'));
}

public static void addCar() {
    // Add the Car adding logic here
}

您可以声明一个可以动态增加大小的,而不是使用Arrayfor storage 。而且您需要在您的主要方法之外将其声明为实例变量。CarsArrayList

于 2012-10-23T15:23:56.813 回答