Java - 代码跳过输入

时间:2012-03-10 20:05:30

标签: java

我的任务是为大学创建一个小型应用程序,并创建了一个简单的预订系统。 我有一个问题,当程序运行时,我无法理解它正在执行方法的打印部分,但后来跳转到下一个方法而不执行原始方法的in.next()部分。

我在这里按顺序包括抽象类,发生问题的类,菜单和'Creation'类。

我之前已被告知我的编码(至少命名惯例)不是最好的,所以我提前为此道歉。

当我改变了类而不是raw_Booking的扩展时,它似乎没有这个问题,但不确定它是否相关。

import java.util.Scanner;


abstract class raw_Booking {


private static String fname;
private static String lname;
private static int HouseNo;
private static String Street;
private static String Postcode;


Scanner in = new Scanner(System.in);


raw_Booking(){


}

abstract public String Setfname();
abstract public String Setlname();
abstract public int SetHouseNo();
abstract public String SetStreet();
abstract public String SetPostcode();
abstract public String Verification();



}

问题类(方法是SetPostcode):

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

class Hotel_Booking extends raw_Booking{



Scanner in = new Scanner(System.in);

private String TheVerification;
private int guests;
private String Roomtype;
private double numbRooms;
private double costStay;
private double nights;
private static String fname;
private static String lname;
private static int HouseNo;
private static String Street;
private static String Postcode;

Hotel_Booking() {

    fname = Setfname();  
    lname = Setlname();
    HouseNo = SetHouseNo();
    Street = SetStreet();
    Postcode = SetPostcode();
    TheVerification = Verification();
    Roomtype = setRoomtype();
    guests = guests();
    numbRooms = numbRooms();
    nights = nights();
    costStay = costStay();
    Write_File();
}


public String Setfname(){
    System.out.println();
    System.out.print("Bookers first name please:   ");
    fname = in.next();
    return fname;
}

public String Setlname(){

    System.out.println();
    System.out.print("Bookers surname please:   ");
    lname = in.next();
    return lname;
}

public int SetHouseNo(){
   Scanner sc = new Scanner(System.in);
   System.out.println();
   System.out.print("Please enter your house number:   ");
   while (!sc.hasNextInt()) {
       System.out.println();
       System.out.print("You need to enter a number - please re-enter:   ");
       sc.next();
   }
return sc.nextInt();
}


public String SetStreet(){
    System.out.println();
    System.out.print("Please enter your street:   ");
    Street =in.next();
    return Street;
}

public String SetPostcode(){

    System.out.println();
    System.out.print("Please enter your postcode:   ");
    Postcode = in.next();
    return Postcode;
}


public String Verification(){

    System.out.println();
    System.out.print("Please provide your car reg as verification:   ");
    TheVerification = in.next();
    return TheVerification;

}

public String setRoomtype(){  

    System.out.println();
    System.out.print("Would you like a Premiun or Regular room(s)?   ");
    Roomtype = in.next();
    return Roomtype;
}

public int guests(){

    System.out.println();
    System.out.print("How many guests will be staying?   ");
    guests = in.nextInt();
    return guests;

}
//For every 3 guests their must be 1 room at £50 per night.
//Premiun rooms = price doubled

public double numbRooms(){

   for(int i=0;i < guests;i++){
       numbRooms = numbRooms+1;
       i = i + 2;
    }
   System.out.println();
   System.out.println("You will require " + numbRooms + " number of rooms");
   return numbRooms;
}

public double nights(){

    System.out.println();
    System.out.print("How many nights are you staying?   ");
    nights = in.nextDouble();
    return nights;
}

public double costStay(){

    if(Roomtype.equals("Regular")){
        costStay = (nights * numbRooms)*50;
    }
    else{
        costStay = (nights * numbRooms)*100;
    }
    System.out.println();
    System.out.println("The total cost of your stay will be: " + costStay);
    return costStay;
}

public void Write_File(){
    System.out.println();
    System.out.println("Your details will now be written to file.");
    System.out.println();
    try{
        PrintWriter wr = new PrintWriter(new    FileOutputStream("Hotel_Booking.txt",true));
        wr.println("Name: " + fname+ " " + lname);
        wr.println("Address line one: " + HouseNo + " " + Street);
        wr.println("Postcode: " + Postcode);
        wr.println("Roomtype: " + Roomtype);
        wr.println("Number of rooms: " + numbRooms);
        wr.println("Nights staying: " + nights);
        wr.println("Total cost of stay: " + costStay);
        wr.close();
       }

    catch (IOException e){
        System.out.println("Error -- " + e.toString());
        }

}
public static String Getfname(){
    return fname;
}

 }

包含界面/菜单..

public class Interface{



 public static void Menu(){

    System.out.println("Welcome to the booking system select an option");
    System.out.println("1 - Create a new hotel booking");
    System.out.println("2 - Create a new airline booking");
    System.out.println("3 - Create a car hire booking");
    System.out.println("4 - Create a amusement park ticket booking");
    System.out.println("5 - Create a concert ticket booking");
    System.out.println();




}

}

主要执行点:

import java.util.ArrayList;
import java.util.Scanner;

public class Booking_Creation{

static Scanner in = new Scanner(System.in);    

static ArrayList<Hotel_Booking> hotelbookings = new ArrayList<Hotel_Booking>();
static ArrayList<Flight_Booking> flightbookings = new ArrayList<Flight_Booking>();
static ArrayList<Car_Hire> carhirebookings = new ArrayList<Car_Hire>();
static ArrayList<Amusement_Park_Ticket> parkticket = new ArrayList<Amusement_Park_Ticket>();
static ArrayList<Concert_Ticket> concertticket = new ArrayList<Concert_Ticket>();



static int selection;
public static void main(String[] arguments){

    Interface.Menu();

    selection = in.nextInt();

    // COULD ASK THE QUESTIONS HERE????
    /// BUT THE INPUT BIT IS INSIDE THE OTHER CLASS



    switch(selection){
            case 1:
            Hotel_Booking hb = new Hotel_Booking();
            hotelbookings.add(hb);
            break;
            case 2:
            Flight_Booking fb = new Flight_Booking();
            flightbookings.add(fb);
            break;
            case 3:
            Car_Hire ch = new Car_Hire();
            carhirebookings.add(ch);
            break;
            case 4:
            Amusement_Park_Ticket pt = new Amusement_Park_Ticket();
            parkticket.add(pt);
            break;
            case 5:
            Concert_Ticket ct = new Concert_Ticket();
            concertticket.add(ct);
            break;
        }


}
}

示例输出:

Welcome to the booking system select an option
1 - Create a new hotel booking
2 - Create a new airline booking
3 - Create a car hire booking
4 - Create a amusement park ticket booking
5 - Create a concert ticket booking

1

Bookers first name please:   Herton

Bookers surname please:   Ferford

Please enter your house number:   4

Please enter your street:   Park Lane

Please enter your postcode:   
Please provide your car reg as verification:  KS23

提前问候。

1 个答案:

答案 0 :(得分:1)

快速浏览一下您的示例执行后,我认为街道名称被解析为两个令牌,因此街道名称设置为“Tulloch”,当提示输入邮政编码(作为字符串)时,“Park”是读。 在 Scanner.next()方法上设置模式以强制将整行作为单个标记可能很有用?