在这个java代码中,如何正确打印概述中的名称数组?

时间:2016-10-31 16:19:25

标签: java

我唯一的问题是在概述部分如何正确打印出拖车租赁名称?像这样:

sample

但是我的概述代码并没有完全正常工作。问题是什么? (我理解从零开始的数组计数)

import java.util.Scanner;      

public class TrailerRental {

    Scanner kb=new Scanner(System.in);
    int weightSmall=750;
    int weightLarge=3000;
    int smallTrailerCounter =0;
    int largeTrailerCounter =0;
    String smallClientName;
    String largeClientName;
    String [] smallTrailersClients=new String[5];
    String [] largeTrailersClients= new String[3];

    public static void main(String[] args) {
        new TrailerRental().run();
    }

    public void run(){
        menu();
        int menu=kb.nextInt();

        while (menu==1 || menu==2|| menu==3){
            if(menu==1){
                smallTrailer();
            }
            if (menu==2){
                largeTrailer();
            }
            if (menu==3){
                overView();
            }
        }
        if (menu==9){
            System.out.println("Goodbye!");
        }
        else {
            System.out.println("Error, incorrect input try again!");
            System.out.println();
            run();
        }
    }

    public void overView() {
        System.out.println("Overview");
        System.out.println();
        System.out.println("Rented Small trailers");
        int notUsedSmallTrailer=5-smallTrailerCounter;
        for (int i=0;i<smallTrailerCounter;i++){
            System.out.println("Small trailer " +(i+1) + " :" +smallTrailersClients[i] );
        }
        System.out.println("There are "+notUsedSmallTrailer+ "  out of 5 small trailers still available.");
        System.out.println("Rented Large trailers");
        int notUsedLargeTrailer=3-largeTrailerCounter;
        for(int i=0;i<smallTrailerCounter;i++){
            System.out.println("Large trailer " + (i+1) +" : "+largeTrailersClients[i]  );
        }
        System.out.println("There are "+notUsedLargeTrailer+ " out of 3 large trailer still available ");
        run();
    }

    public void largeTrailer() {
        if (largeTrailerCounter>=3){
            System.out.println("Unfortunately all Small trailers are out");
            System.out.println("Would you like to rent a Small trailer instead?(y/n)");
            String answer=kb.next();
            if (answer.equals("y")){
                smallTrailer();
            }
            if (answer.equals("n")){
                run();
            }
            else {
                System.out.println("Incorrect input, return to main menu");
                run();
            }

        } else {
            System.out.println("What is your name?");
            largeClientName=kb.next();
            System.out.println("Is the driver in possession of an E-type drivers license (y/n)?");
            String answer=kb.next();
            if (answer.equals("n")){
                System.out.println("Unfortunately, a large trailer cannot be rented without a E-type drivers license. ");
                run();
            }
            if (answer.equals("y")){
                System.out.println("What is the weight of the load (in kg)?");
                int weight=kb.nextInt();
                if (weight<=weightLarge){
                    largeTrailerCounter++;
                    largeTrailersClients[largeTrailerCounter]=largeClientName;
                    run();
                }
                if (weight>weightLarge){
                    int difference=weight-weightLarge;
                    System.out.println("Warning! the maximum load "+weightLarge+" kg is exceeded by " +difference+ "kg" );
                    System.out.println("Are you sure you want to rent a trailer?(y/n)?");
                    String answer2=kb.next();
                    if (answer2.equals("n")){
                        run();
                    }
                    if (answer2.equals("y")){
                        largeTrailerCounter++;
                        largeTrailersClients[largeTrailerCounter]=largeClientName;
                        run();

                    }
                    else {
                        System.out.println("incorrect input,return to menu");
                        run();
                    }
                }
            }

        }

    }

    public void smallTrailer() {
        if (smallTrailerCounter>=5) {
            System.out.println("Unfortunately all Small trailers are out");
            run();

        } else {
            System.out.println("What is the customer last name?");
            smallClientName = kb.next();
            System.out.println("What is the weight of the load (in kg) ?");
            int weight = kb.nextInt();
            if (weight<=weightSmall){
                smallTrailerCounter++;
                smallTrailersClients[smallTrailerCounter]=smallClientName;
                run();

            }
            if (weight > weightSmall) {
                int difference = weight - weightSmall;
                System.out.println("Warning the maximum load " + weightSmall + " kg is exceeded by " + difference + " kg!");
                System.out.print("Are you sure you want to rent the small trailer? (y/n)");
                String answer = kb.next();
                if (answer.equals("n")) {
                    run();
                }
                if (answer.equals("y")) {
                    smallTrailerCounter++;
                    smallTrailersClients[smallTrailerCounter]=smallClientName;
                    run();

                }
                while (answer != "y" || answer != "n") {
                    System.out.print("Are you sure you want to rent the small trailer? (y/n)");
                    answer = kb.next();
                    if (answer.equals("n")) {
                        run();
                    }
                    if (answer.equals("y")) {
                        run();

                    }
                }
            }
        }
    }

    public void menu() {
        System.out.println("***********************");
        System.out.println("* Trailer rental       ");
        System.out.println("***********************");
        System.out.println("* 1) Rent small trailer");
        System.out.println("* 2) Rent large trailer");
        System.out.println("* 3) Overview          ");
        System.out.println("* 9) Exit              ");
        System.out.println("***********************");
        System.out.print("Select an option:");
    }
}

1 个答案:

答案 0 :(得分:0)

您未向smallTrailersClients[i]largeTrailersClients[i]添加1。您在for loop中引用了i + 1。引用正确的名称需要相同的名称。试试这个:

public void overView() {
    System.out.println("Overview");
    System.out.println();
    System.out.println("Rented Small trailers");
    int notUsedSmallTrailer=5-smallTrailerCounter;
    for (int i=0;i<smallTrailerCounter;i++){
        System.out.println("Small trailer " +(i+1) + " :" +smallTrailersClients[i+1] );
    }
    System.out.println("There are "+notUsedSmallTrailer+ "  out of 5 small trailers still available.");
    System.out.println("Rented Large trailers");
    int notUsedLargeTrailer=3-largeTrailerCounter;
    for(int i=0;i<smallTrailerCounter;i++){
        System.out.println("Large trailer " + (i+1) +" : "+largeTrailersClients[i+1]  );
    }
    System.out.println("There are "+notUsedLargeTrailer+ " out of 3 large trailer still available ");
    run();
}