0

ArrayList当我选择选项“a”时,我可以在我的全部中存储一些值。但是当我转到选项“b”并尝试检索我输入的内容并存储在变量中rq(以检查预订的房间数量)并在选项“b”中使用它进行循环时循环它。但是 all 数组是空的。

我在编译时也有一个“错误”。

在选项 'b' for 循环中,我这样做了

for(int i=0;i<all.get(4);i++)

但我收到一个错误:error: bad operand types for binary operator '<'

所以我决定投它。

for(int i=0;i<(Integer)all.get(4);i++)

现在没有错误,但编译似乎有问题。

注意:C:\Users\MegaStore\Desktop\java testing\RoomSelection.java 使用未经检查或不安全的操作。注意:使用 -Xlint:unchecked 重新编译以获取详细信息。

所以我想知道我输入的那些值是在堆中,还是在我选择选项“b”时被垃圾收集器删除。

我的代码尚未完全输入,但我想解决手头的问题。

我的程序还需要记录用户想要的附加组件(最多 4 个)并存储它。所以我不太确定在哪里添加和存储它。

import java.util.*;
import java.io.*;

public class test123{

public test123()throws InputMismatchException {

    String choiceStr;//initialize choiceStr which is use for reading lines from scanner input
    char choiceChar;//initialize choiceStr which is use for reading character from scanner input
    int choice,rt,rq,ao1,night,ao2,quan;//initialize choiceStr which is use for reading integer from scanner input
    String datee;
    String[] roomType = {"Single Room", "Double Room", "Deluxe Room", "Junior Room", "Suite"}; //Initialize a array for room type
    Integer[] priceRoom = {160,200,280,380,500}; //Initialize a array for room prices
    Integer[] priceAdd = {25,60,70,100}; //Initialize a array for add-on prices
    ArrayList<String> roomAndDate = new ArrayList<String>();
    ArrayList<Integer> all = new ArrayList<Integer>();
    Scanner input= new Scanner(System.in); //Initialize a scanner input

    System.out.println("Room Selection");
    System.out.println("==============\n");
    System.out.println("[a] Room Type");
    System.out.println("[b] Add-Ons");
    System.out.println("[c] Main Menu");
    System.out.println("Type 'a' to select Room Type and state the desire quantity for each type.");
    System.out.println("Type 'b' to select the Add-Ons.");
    System.out.println("Type 'c' to exit from  the Booking Menu.");
    System.out.println("Please enter your option (a, b or c): ");

    ;
    choiceStr = input.nextLine();
    choiceChar = choiceStr.charAt(0); 

    switch(choiceChar){
        case 'a':   System.out.println("Room Type");
                    System.out.println("=====================================================");
                    System.out.println("(1) Single Room (1 person) - Price: S$160");
                    System.out.println("(2) Double Room (2 persons) - Price: S$200");
                    System.out.println("(3) Deluxe Room (2 persons) - Price: S$280");
                    System.out.println("(4) Junior Suite (2 persons) - Price: S$380");
                    System.out.println("(5) Suite (2 persons) - Price: S$500\n");
                    System.out.println("Enter Room types (Enter '1' to '5')");
                    choice=input.nextInt();
                    while(choice>5){
                        if(choice>5){
                        System.out.println("Please enter number between '1' to '5'!");
                        choice=input.nextInt();
                        }                   
                    }

                    String roomTypess= roomType[choice-1];
                    roomAndDate.add(roomTypess);
                    int storePricee = priceRoom[choice-1];
                    all.add(storePricee);

                    System.out.println("Number of rooms required (maximum 10): ");
                    choice=input.nextInt();
                    while(choice>10){       
                        if(choice>10){
                        System.out.println("Please enter again!");
                        choice=input.nextInt();
                        }
                    }
                    all.add(choice);

                    for(int i=0;i<choice;i++){
                        System.out.println("Enter the date of checked-in (dd/mm/yy) for "+roomAndDate.get(0) +" " + (i+1));
                        choiceStr = input.nextLine();
                        choiceStr = input.nextLine();
                        roomAndDate.add(choiceStr);
                        System.out.println(roomAndDate);
                        System.out.println("Enter number of Add-on for "+roomAndDate.get(0) + " " +(i+1)+": ");
                        choice=input.nextInt();
                        while(choice>4){
                        if(choice>4){   
                            System.out.println("Please enter again! Choose only option 1 to 4");
                            choice=input.nextInt();
                            }                           
                        }
                        all.add(choice);            
                        System.out.println("Number of night(s) required (maximum 30) for "+roomAndDate.get(0) + " " +(i+1)+ ": ");
                        choice=input.nextInt();
                        while(choice>30){   
                            if(choice>30){
                            System.out.println("Please enter again! Maximum is 30 days!");
                            choice=input.nextInt();
                            }
                        }           
                        all.add(choice);


                    }
                    new RoomSelection();

                    break;          
        case 'b':       System.out.println("Room req is empty? : "+all.isEmpty());
                        System.out.println("Add-Ons");
                        System.out.println("=====================================================");
                        System.out.println("(1) Breakfast voucher (1 person) per day - Price: S$25");
                        System.out.println("(2) Spa voucher (1 person) - Price: S$60");
                        System.out.println("(3) Half Day Tour voucher (1 person) - Price: S$70");
                        System.out.println("(4) Full Day Tour voucher (1 person) - Price: $100\n");



                        for(int i=0;i<(Integer)all.get(3);i++){

                        System.out.println("Enter Add-On option");
                        choice=input.nextInt();
                        while(choice>4){
                            if(choice>4){   
                                System.out.println("Please enter again! Choose only option 1 to 4");
                                choice=input.nextInt();
                            }                           
                        }
                        all.add(choice);
                        System.out.println("Enter quantity required for Add-On option " + (i+1)+": ");
                        choice=input.nextInt();
                        all.add(choice);
                        }

                    break;
        case 'c':   new MainPage1();break;
        default:continue;
    }

}

}
4

3 回答 3

3

这就是为什么你应该使用泛型。目前,查看 的声明all,编译器和读者都无法看到其中的含义。查看代码,它似乎是字符串和整数的混合体。这是灾难的秘诀。它首先真正代表的是什么?为什么它包含字符串和整数的混合?

听起来你很可能想要一个Room包含所有这些信息的类型,并且all应该像这样声明:

List<Room> allRooms = new ArrayList<Room>();

此外,这种方法目前太长了当一条线超过大约 20 条线时,我会感到紧张……但是超过 130 条线?哎呀!找出可以提取哪些位 - 那些不影响其余代码的位。

哦,学习数组初始化器,把它变成:

Integer[] priceAdd = new Integer[4]; //Initialize a array for add-on prices
priceAdd[0]=25; //To be added to array add-on price
priceAdd[1]=60; //To be added to array add-on price
priceAdd[2]=70; //To be added to array add-on price
priceAdd[3]=100; //To be added to array add-on price      

进入这个:

int[] addOnPrices = { 25, 60, 70, 100 };

(我看不出有什么理由需要它们Integer而不是int,尽管您目前假设您永远不需要小数值......)

于 2012-07-25T17:18:53.037 回答
1

List<Integer> all = new ArrayList<Integer>();

以上将始终接受并返回整数值。

于 2012-07-25T17:52:01.657 回答
1

您需要像这样定义 ArrayList 的类型:

ArrayList<String> all = new ArrayList<String>();

或使用您使用的任何非原始数据类型。

于 2012-07-25T17:19:02.990 回答