【问题标题】:(Beginner) Java program terminating for no reason(初学者)Java程序无故终止
【发布时间】:2014-10-03 14:16:27
【问题描述】:

我目前正在开发一个酒店预订练习程序(我是 Java 新手,所以我正在通过制作随机程序来练习)对于我的一生,我无法弄清楚为什么程序在我输入我的号码后终止客人,我在 Eclipse 中编写程序,我也在 E​​clipse 中编译程序。任何帮助将不胜感激。

我的代码:

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

【问题讨论】:

  • 它会打印一些东西然后立即终止,因为程序就是这样写的
  • @1337 你能解释一下为什么会这样吗?我真的是 Java 新手。
  • 看这里stackoverflow.com/questions/7416018/…如果还是想不通,我可以在这里详细说明
  • 因为打印文本后没有更多代码了。我猜你想加入一个循环。
  • 花点时间阅读 Oracle 提供的免费 online Tutorial。在这种情况下,while loopsfor loopsscanning 上的页面。

标签: java eclipse


【解决方案1】:

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

不建议使用相同的扫描仪来读取整数和 阅读文本输入

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

【讨论】:

  • 非常感谢,这解决了我的问题! :D
猜你喜欢
  • 1970-01-01
  • 2015-04-11
  • 2013-04-23
  • 2013-04-23
  • 2012-04-14
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 2014-11-12
相关资源
最近更新 更多