在布尔数组列表中查询值

时间:2017-04-06 00:14:40

标签: java

我必须编写一个程序,显示一些摩托车,然后客户租一辆摩托车,这辆自行车不再可用。然后我必须记录租房时间,当一个人返回时,它有记录返回时间并计算成本。我是一个菜鸟程序员。如果有人能够在简单的事情上解释我怎么能完成这个。我写过这样的东西。谢谢你的时间。

Scanner s = new Scanner(System.in);
byte numBikes = 15;

boolean[] available = new boolean[numBikes];
String [] timeRented = new String [numBikes];
Arrays.fill(available , true);  


System.out.println("The following bikes are avaialable:\n");
for(byte b = 0; b <= numBikes - 1; b++)
    if(available [b]== true){
        System.out.println(" Bike No" + (byte)(b+1));

    }
System.out.print("Enter your bike number you want choose");
byte bikeNo = s.nextByte();

if (available[bikeNo - 1]== true)
    available[bikeNo - 1] = false;

System.out.println("The following bikes are avaialable:\n");
for(byte b = 0; b <= numBikes - 1; b++)
    if(available [b]== true){
        System.out.println(" Bike No" + (byte)(b+1));
    }

1 个答案:

答案 0 :(得分:0)

我为你做了一个样本我提示用户计算成本,因为我不知道你想要如何计算成本我不知道你的租金但希望你可以在字母b中改变它选择

import java.util.*;
    public class Help
    {

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int numBikes =15;
        int availableBike=15;
        int bikeFound=0;

        boolean[] available = new boolean[numBikes];
         String[] timeRented = new String [numBikes];
         String[] returningTime = new String[numBikes];
         int[] calculateCost = new int[numBikes];
         Arrays.fill(available,true);
         String choice="";
         do
         {
             System.out.println("What do you want to do zzz to quit?");
             System.out.println("a.rent a bike b. return a bike");
             choice = scan.nextLine();
             if(choice.charAt(0)=='a')
             {
                System.out.println("The following bikes are available:\n");
                for(int b = 0; b <= numBikes - 1; b++)
                    if(available [b]== true)                
                        System.out.println(" Bike No:" + (b+1));
                System.out.println("Enter your bike number you want choose");
                int bikeNo = scan.nextInt();
                scan.nextLine();
                if(availableBike>0)
                {
                    if(available[bikeNo-1]==true)
                    {                   
                        available[bikeNo-1]=false;
                        System.out.println("Enter time of renting");
                        timeRented[bikeNo-1]=scan.nextLine();
                        --availableBike;
                    }
                    else
                        System.out.println("Sorry bike's not available choose another");
                }
                else
                    System.out.println("Sorry all bikes are not available at this time");
             }
            else if(choice.charAt(0)=='b')
            {
                System.out.println("The following bikes are not available:\n");
                for(int b = 0; b <= numBikes - 1; b++)
                    if(available [b]== false)               
                        System.out.println(" Bike No:" + (b+1));

                System.out.println("Enter bike number you want to return");
                int bikeNo=scan.nextInt();
                scan.nextLine();
                if(available[bikeNo-1]==false)
                {
                    available[bikeNo-1]=true;
                    System.out.println("Enter out time");
                    returningTime[bikeNo-1]=scan.nextLine();
                    System.out.println("Enter cost total");
                    calculateCost[bikeNo-1]=scan.nextInt();
                    scan.nextLine();
                    System.out.println("Total is:"+calculateCost[bikeNo-1]);
                    calculateCost[bikeNo-1]=0;
                    ++availableBike;
                }
                else
                    System.out.println("Sorry error");
            }
         }
         while(!(choice.equals("zzz")));

    }

}