ArrayList的循环 [英] ArrayList looping

查看:101
本文介绍了ArrayList的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在订房的菜单,我有主有3个选项:A,B和C。
选项​​'一' - 舒适型的选择。 5种不同房型可供选择。
选项​​'b'-选择的附加组件每个room.Each插件可以选择额外的费用。
选C - 从make预订页面返回到主页

我的程序需要显示5种不同类型的房间。此时,客户只允许选择的选项A,B或C。

如果顾客进入和无效的数量是在选择房间时,没有1至5包容性,系统应该给错误信息并提示用户输入again.Once用户输入他/她想要,他们可以继续进入的房间类型量为每种类型的房间。
房型的选择后,系统会提示您选择附加组件每种房型。

这是我的code below.I着让我的加载项我存储在从选项整数ArrayList的编号'a',当用户选择选项'B'。

我初始化数组列表2 IM,但不知道他们是否正确。第一个ArrayList是存储字符串是roomtype和日期。第二个ArrayList是整数,它存储的房间价格,需要的房间号码,附加组件数量,住宿天数,附加选项,用户选择和数量附加的。

我很新的编码等都需要它的帮助。

 进口的java.util。*;
进口java.io. *;公共类RoomSelection {    公共RoomSelection()抛出InputMismatchException时{        字符串choiceStr; //初始化choiceStr这是用于读取来自扫描仪输入线
        焦炭choiceChar; //初始化choiceStr这是使用了从扫描仪输入读取字符
        INT选择; //初始化choiceStr这是使用了从扫描仪输入读取整
        字符串显示日期;
        的String [] = roomType {单人间,双人间,豪华间,普通房,套房}; //初始化为房间类型数组
        整数[] = priceRoom {160,200,280,380,500}; //初始化房价格阵列
        整数[] = priceAdd 25 {60,70,100}; //初始化为附加价格阵列        ArrayList的<串GT; roomAndDate =新的ArrayList<串GT;();
        ArrayList的<整数GT;所有=新的ArrayList<整数GT;();
        扫描仪输入=新的扫描仪(System.in); //初始化扫描仪输入        的System.out.println(选房);
        的System.out.println(============== \\ n);
        的System.out.println([A]房型);
        的System.out.println([B]加载项);
        的System.out.println([C]主菜单);
        的System.out.println('A'型来选择房间类型和状态的渴望量为每种类型。);
        的System.out.println('B'型来选择附加组件。);
        的System.out.println(类型C从预订菜单中退出。);
        的System.out.println(请输入您的选项(A,B或C):);        choiceStr = input.nextLine();
        choiceChar = choiceStr.charAt(0);
        而(真){            开关(choiceChar){
                案一:
                    的System.out.println(房间);
                    的System.out.println(=========================================== ==========);
                    的System.out.println((1)单人间(1人) - 价格:$ 160);
                    的System.out.println((2)双人房(2人) - 价格:200 $);
                    的System.out.println((3)豪华房(2人) - 价格:$ 280);
                    的System.out.println((4)小型套房(2人) - 价格:$ 380);
                    的System.out.println((5)套房(2人) - 价格:500 $ \\ n);
                    的System.out.println(进入会议室类型(输入1,5));
                    选择= input.nextInt();
                    而(选择大于5){
                        如果(选择大于5){
                            的System.out.println(请'1'之间输入号码5!);
                            选择= input.nextInt();
                        }
                    }                    字符串roomTypess = roomType [选择 - 1];
                    roomAndDate.add(roomTypess);
                    INT storePricee = priceRoom [选择 - 1];
                    all.add(storePricee);                    的System.out.println(所需的客房数量(最多10个):);
                    选择= input.nextInt();
                    而(选择→10){
                        如果(选择→10){
                            的System.out.println(请重新输入!);
                            选择= input.nextInt();
                        }
                    }
                    all.add(选择);                    的for(int i = 0; I<选择;我++){
                        的System.out.println(输入签入(DD / MM / YY日期)为+ roomAndDate.get(0)++(I + 1));
                        choiceStr = input.nextLine();
                        choiceStr = input.nextLine();
                        roomAndDate.add(choiceStr);
                        的System.out.println(roomAndDate);
                        的System.out.println(输入数的附加组件+ roomAndDate.get(0)++(I + 1)+:);
                        选择= input.nextInt();
                        而(选择> 4){
                            如果(选择> 4){
                                的System.out.println(请重新输入选择的唯一选择1至4!);
                                选择= input.nextInt();
                            }
                        }
                        all.add(选择);
                        的System.out.println(所需晚(最多30)号+ roomAndDate.get(0)++(I + 1)+:);
                        选择= input.nextInt();
                        而(选择大于30){
                            如果(选择大于30){
                                的System.out.println(请重新输入最多为30天!);
                                选择= input.nextInt();
                            }
                        }
                        all.add(选择);
                    }                    新RoomSelection();
                    打破;
                案例'B':
                    的System.out.println(插件);
                    的System.out.println(=========================================== ==========);
                    的System.out.println(每天(1)早餐券(1人) - 价格:$ 25);
                    的System.out.println((2)水疗券(1人) - 价格:$ 60);
                    的System.out.println((3)半天游览券(1人) - 价格:$ 70);
                    的System.out.println((4)全日游券(1人) - 价格:100 $ \\ n);                    对(INT I = 0; I&≤(整数)all.get(3);我++){                        的System.out.println(输入的附加选项);
                        选择= input.nextInt();
                        而(选择> 4){
                            如果(选择> 4){
                                的System.out.println(请重新输入选择的唯一选择1至4!);
                                选择= input.nextInt();
                            }
                        }
                        all.add(选择);
                        的System.out.println(输入的附加选项所需数量+(I + 1)+:);
                        选择= input.nextInt();
                        all.add(选择);
                    }
                    打破;
                情况下C:
                    新MainPage1();
                    打破;
                默认:
                    继续;
            }
        }
    }
}


解决方案

我看看你的code和有一些东西你不需要和你需要提高。


  1. 而不是读一个整数 input.nextInt(); 你应该阅读输入字符串并尝试执行的Integer.parseInt(yourString),然后用这个它(如果它抛出一个 NumberFormatException异常你就会知道该字符串不是有效的整数)。

  2. ,而循环,你应该排除值的情况下 0

  3. 消除如果语句从,而语句作为,而情况纳入如果在里面。

  4. 创建了有你需要的不是有两个数组列表功能对象的数组列表:

    • 创建并具有作为场房,成本类型的对象,....

    • 定义和分配 RoomSelection之外的对象()的功能,这样你将有机会获得这一切的时候这个数组列表。

    • 如果您在 RoomSelection定义它()你的名单做,当你调用新RoomSelection(); 创建列表,而不是使用现有的新实例。


希望我是一定的帮助。最后一点是非常重要的。

In Booking menu, i have main with 3 options:a,b and c. Option 'a' - selection of room types. 5 different room types to choose from. Option 'b'- selection of add-ons for each room.Each add-on can be selected for additional cost. Option 'c' - return from make booking page to main page

My program needs to display 5 different types of room. At this point customer is only allowed to select option 'a', 'b' or 'c'.

If customer enters and invalid number which is not 1 to 5 inclusive when selecting room ,system should give error message and prompt user to enter again.Once the user entered the type of room he/she wants, they can proceed to enter quantity for each type of room. After the selection of room type, the system will prompt you to select add-ons for each room type.

This is my code below.I cant to get my number of add-ons i stored in the integer arraylist from option 'a' when the user selection option 'b'.

I initialized 2 arraylist but im not sure whether they are correct. The first arraylist is to store strings which is roomtype and date. The second arraylist is integer, which stores room price,number of room require, number of add-ons,number of nights,add-on option the user selects and quantity of the add-on.

I'm quite new to coding so need help on it.

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

public class RoomSelection {

    public RoomSelection() 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;//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);
        while (true) {

            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("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;
            }
        }
    }
}

解决方案

I had a look at your code and there are some things you don't need and that you need to improve.

  1. Instead of reading an integer input.nextInt(); you should read the input string and try to perform Integer.parseInt(yourString) and use the resulting it (if it throws a NumberFormatException you'll know the string isn't a valid integer).
  2. for the while loops you should exclude the case for value 0
  3. eliminate the if statements from inside the while statements as the while condition incorporate the if in it.
  4. Create an array list of objects that have the features that you require instead of having two array lists:
    • create and object that has as fields the type of room, cost, ....
    • define and allocate this array list of objects outside the RoomSelection() function such that you will have access to it all the time.
    • if you define it in the RoomSelection() as you do with the lists, when you call new RoomSelection(); you create new instances of the lists instead of using the existing ones.

Hope I was of some help. The last point is very important.

这篇关于ArrayList的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆