我目前正致力于酒店预订实践计划(我是Java的新手,所以我通过制作随机程序来练习)对于我的生活,我无法弄清楚为什么这个程序在我输入我的客人数后终止,我在Eclipse中编写了程序,我也在Eclipse中编译程序。任何帮助将不胜感激。
我的代码:
package text;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import org.apache.commons.lang3.ArrayUtils;
public class HotelReservations {
public static void main(String[] args){
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
int Guests;
String Reserved;
int[] RoomsArray = new int[10];
RoomsArray[0] = (int) ((10 * Math.random()) - 1);
RoomsArray[1] = (int) ((10 * Math.random()) - 1);
RoomsArray[2] = (int) ((10 * Math.random()) - 1);
RoomsArray[3] = (int) ((10 * Math.random()) - 1);
RoomsArray[4] = (int) ((10 * Math.random()) - 1);
RoomsArray[5] = (int) ((10 * Math.random()) - 1);
RoomsArray[6] = (int) ((10 * Math.random()) - 1);
RoomsArray[7] = (int) ((10 * Math.random()) - 1);
RoomsArray[8] = (int) ((10 * Math.random()) - 1);
RoomsArray[9] = (int) ((10 * Math.random()) - 1);
Date Date = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("EEE, d MMM yyyy 'at' hh:mm a!");
System.out.println("Today's date is: " + ft.format(Date));
System.out.println();
System.out.println("Enter the number of guests:");
Guests = in.nextInt();
if (Guests > 8)
{
System.out.println("The maximum amount of guests allowed per room is 8. Please split groups of more than 8 into seperate rooms.");
System.exit(0);
}
else if (ArrayUtils.contains(RoomsArray, 0))
{
System.out.println("A room is available! The closest available room is: Room " + ArrayUtils.indexOf(RoomsArray, 0));
}
else
{
System.out.println("Sorry, no rooms are available.");
}
System.out.println("Would you like to reserve this room? (Yes/No)");
Reserved = in.nextLine();
if (Reserved.equals("Yes")) //here is where i changed it, now it will check yes/no
{
System.out.println("Hi");
} else
{
System.out.println("No");
}
}
}
答案 0 :(得分:2)
我想您正在尝试使用相同的 scanner 来读取整数和文本的输入
不建议使用相同的扫描仪来读取整数和 阅读文字输入
使用不同的扫描仪,如scan1和scan2
scan1表示整数
scan2将下一行作为字符串
Scanner scan1= new Scanner(System.in);
System.out.println(q.nextInt());
Scanner scan2= new Scanner(System.in);
System.out.println(scan2.nextLine());