gpt4 book ai didi

java - (初学者)Java程序无故终止

转载 作者:行者123 更新时间:2023-11-30 03:47:37 24 4
gpt4 key购买 nike

我目前正在开发一个酒店预订练习程序(我是 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");
}
}
}

最佳答案

我猜您正在尝试使用相同的scanner--in来读取整数和文本的输入

It is not recommended to use same scanner for reading integers and reading text input

使用不同的扫描仪,例如 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());

关于java - (初学者)Java程序无故终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25231643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com