再次运行循环,并在输出后将控件传递给 Java 中的 Main 类



我正在尝试制作一个简单的JAVA程序,以帮助用户选择他选择的汽车。

public class CarSelector {
static CarSelector start = new CarSelector();
public String BodyType(String a){
    String hatchBack, SUV, MUV, compactSedan, sedan, saloon, miniVan, convertible, hybrid, coupe;
    if(a.equalsIgnoreCase("a")){
        hatchBack = "polo";
        System.out.println("We recommend: " +hatchBack);
    }
    String b = "";
    if(a.equalsIgnoreCase("b")){
        SUV = "Fortuner";
        System.out.println("We recommend: " +SUV);
    }
    if(a.equalsIgnoreCase("c")){
        compactSedan = "Amaze";
        System.out.println("We recommend: " +compactSedan);
    }
    if(a.equalsIgnoreCase("d")){
        sedan = "Vento";
        System.out.println("We recommend: " +sedan);
    }
    if(a.equalsIgnoreCase("e")){
        saloon = "Corolla";
        System.out.println("We recommend: " +saloon);
    }
    if(a.equalsIgnoreCase("f")){
        MUV = "Innova";
        System.out.println("We recommend: " +MUV);
    }
    else{
        System.out.println("Incorrect choice.");
        System.out.println(a);
        //start.BodyType(a);
        return null;
    }
    System.out.println("We recommend: " +a);
    return null ;
}
public int PriceRange(){
    int price5 = 5;
    int price10 = 10;
    int price15 = 15;
    int price20 = 20;
    int price25 = 25;
    int price30 = 30;

    return 0 ;
}
public String SegmentBest(){
    //string type of the best cars within price range
    return null;
}
public int OnRoadPrice(){
    //return int of on road price
    return 0;
}
public String Manufacturer(){
    //all manufacturers with their models available
    String Toyota, Volkswagen, Honda;
    String i1= "Toyota";
    String i2= "Volkswagen";
    String i3= "Honda";
    return null;
}
public int SeatingCapacity(){
    //return integer seating capacity
    return 0;
}
public String ReviewLink(){
    return null;
}
public String LatestReleases(){
    return null;
}
public String FuelType(){
    return null;
}
public static void main (String[] args){

    Scanner input = new Scanner(System.in);
    String option;
    System.out.println("Welcome to car selector: ");
    System.out.println("Choose according to: ");
    System.out.println("A:Body Type");
    System.out.println("B: Manufacturer");
    System.out.println("C: Price Range");
    option = input.nextLine();
    if( option.equalsIgnoreCase("a")){
        System.out.println("A: Hatchback");
        System.out.println("B: SUV");
        System.out.println("C: MUV");
        System.out.println("D: Sedan");
        System.out.println("E: Saloon");
        System.out.println("F: Compact Sedan");
        String optionA = input.nextLine();
        start.BodyType(optionA);
    }

}

}

代码很简单。演练:主类将提示用户选择他想要如何选择汽车。给定选项"A"作为选择将运行第一种方法。这是我的查询

  • 在 BodyType 方法中,如果用户输入 a,b,c,d,e,f 以外的任何内容,我想再次运行 IF 语句集
  • 如何将控件交还给主类(从 MAIN 方法运行特定代码),并从另一个方法(从 BodyType 到 PriceRange)启动方法。我希望我说得很清楚。谢谢,干杯!

你现在可以玩了...

import java.util.Scanner;
public class CarSelector {
    static CarSelector start = new CarSelector();
    static String[] bodytypes = new String[]{"hatchBack", "SUV", "MUV", "compactSedan", "sedan",
            "saloon", "miniVan", "convertible", "hybrid", "coupe"};
    static String[] manufacturers = new String[]{"Toyota", "Volkswagen", "Honda"};
    private String getBodyType(int bt) {
        if(bt >= bodytypes.length){
            System.err.println("Incorrect choice.");
            return null;
        }else{
            System.out.println("We recommend: " + bodytypes[bt]);
            return bodytypes[bt];
        }
    }
    public static String getManufacturer(int mf) {
        if(mf >= manufacturers.length){
            System.err.println("Incorrect choice.");
            return null;
        }else{
            System.out.println("We recommend: " + manufacturers[mf]);
            return manufacturers[mf];
        }
    }
    public static void main(String[] args) {
        String bodyType = "";
        String manufacturer = "";
        Scanner input = new Scanner(System.in);
        String option;
        System.out.println("Welcome to car selector: ");
        System.out.println("Choose according to: ");
        pringSelectType();
        option = input.nextLine();
        while(!option.equalsIgnoreCase("o")){
            if (option.equalsIgnoreCase("a")) {
                for(int a = 0; a < bodytypes.length ; a++){
                    System.out.println(a+": "+ bodytypes[a]);
                }
                option = input.nextLine();
                bodyType = start.getBodyType(Integer.parseInt(option));
                pringSelectType();
                option = input.nextLine();
            }else if (option.equalsIgnoreCase("b")) {
                for(int a = 0; a < manufacturers.length ; a++){
                    System.out.println(a+": "+ manufacturers[a]);
                }
                option = input.nextLine();
                manufacturer = getManufacturer(Integer.parseInt(option));
                pringSelectType();
                option = input.nextLine();
            }else{
                option = input.nextLine();
                System.err.println(("input a right choice"));
                pringSelectType();
                option = input.nextLine();
            }
        }
        System.out.println("");
        System.out.println("it's your choice below: ");
        System.out.println("bodyType : "+ bodyType);
        System.out.println("manufacturer : "+ manufacturer);
    }
    private static void pringSelectType() {
        System.out.println("A:Body Type");
        System.out.println("B: Manufacturer");
        System.out.println("C: Price Range");
    }
}

我删除了一些未使用的方法,并更改了一些代码。我没有做任何笔记,因为我认为这很容易。

如果你有什么问题,评论它,我会看到。

PS:当您想通过选择系统时,输入 o.it 将完成。

相关内容

  • 没有找到相关文章

最新更新