我的程式是選單驅動的。有一些錯誤阻止我運行代碼,我無法弄清楚。
我的節目是關于什么的?它應該是一個陣列解決方案。它使用一個選單,允許用戶輸入客戶詳細資訊并根據所選選項進行其他表演。在主要方法中,我已經提到了每個選項的作用。
我完成了大部分要求,但我的代碼無法運行。如果有人可以建議我解決此錯誤的方法,我將不勝感激。
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class Main {
static Scanner scanner = new Scanner(System.in);
static String roomName; //initialising the variables
static int roomNum = 0;
static String[] hotel = new String[12];
static int element;
public static void main(String[] args) { //menu of program
System.out.println("A) Add a customer");
System.out.println("V) View all cabins");
System.out.println("E) Display Empty cabins");
System.out.println("D) Delete customer from cabin");
System.out.println("F) Find cabin from customer name ");
System.out.println("S) Store program data into file");
System.out.println("L) Load program data from file");
System.out.println("O) View passengers ordered alphabetically by name");
System.out.println("Q) Quit");
String options = null;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Cruise Ship Menu Program");
do { //options for menu
options = scan.nextLine();
switch (options) {
case "A":
customerAdd();
break;
case "V":
cabinView();
break;
case "E":
viewEmptyCabins();
break;
case "D":
customerDelete();
break;
case "F":
findCabinFromCustomerName();
break;
case "S":
storeDataToFile();
break;
case "L":
viewDataFromFile();
break;
case "O":
viewPassengersInOrder();
break;
}
} while (!options.equals("Q")); // quitting the program
}
public static void customerAdd(){ //adding customers
System.out.println("Enter room number (0-11) or 12 to stop:");
String value = input.nextLine();
try {
roomNum = Integer.parseInt(value);
if (roomNum >= 0 && roomNum < hotel.length) {
System.out.println("Enter name for room " roomNum " :");
roomName = input.nextLine();
hotel[roomNum] = roomName;
} else if (roomNum < 0 || roomNum > 12) {
System.out.println(roomNum " is not a valid room number");
}
} catch (NumberFormatException exp) {
System.out.println(value " is not a valid room number");
}
System.out.println("");
} while (roomNum != 12);
private static void initialise(String hotelRef[]) {
for (int x = 0; x < hotelRef.length; x ) {
hotelRef[x] = "e";
}
}
public static void cabinView(){ //viewing cabin
initialise(hotel);
do {
for (int x = 0; x < hotel.length; x ) {
if (hotel[x].equals("e")) {
System.out.println("room " x " is empty");
} else {
System.out.println("room " x " is occupied by " hotel[x]);
}
}
}
}
public static void viewEmptyCabins(){
//code here
}
public static void customerDelete(String[] args){
Scanner in;
in = new Scanner(System.in);
String hotel[]
System.out.print("Enter name to delete: ");
int element = in.nextInt();
}
public static void findCabinFromCustomerName(){
//code here
}
public static void storeDataToFile(String args[])throws Exception{
String[] myArray = customerAdd()
BufferedWriter writer = new BufferedWriter(new FileWriter("File.txt", false));
for (String myArray1 : myArray) {
writer.write(myArray1);
writer.newLine();
}
writer.flush();
System.out.println("Successful..!");
}
}
//code here
public static void viewDataFromFile(String[] args)throws Exception
{
File file = new File("C:\\Users\\xyz\\Desktop\\File.txt"); //passing file location
Scanner sc = new Scanner(file);
while (sc.hasNextLine())
System.out.println(sc.nextLine());
}
}
public static void viewPassengersInOrder(){
String[] hotel
for (int i = 0; i < 12; i ) {
System.out.println(seasons[i]);
}
}
}
uj5u.com熱心網友回復:
讓我們快速看一下核心問題。首先列印哪些房間是空的,向用戶詢問一些資料并再次列印所有房間......
while (roomNum < 12) {
for (int x = 0; x < 12; x ) {
if (hotel[x].equals("e")) {
System.out.println("room " x " is empty");
}
}
//...
for (int x = 0; x < 12; x ) {
System.out.println("room " x " occupied by " hotel[x]);
}
}
我覺得這只是令人困惑。只需在要求用戶輸入之前列印所有房間資料...
for (int x = 0; x < hotel.length; x ) {
if (hotel[x].equals("e")) {
System.out.println("room " x " is empty");
} else {
System.out.println("room " x " is occupied by " hotel[x]);
}
}
這將顯示用戶真正需要的所有資訊。
讓我們再深入一點……
System.out.println("Enter room number (0-11) or 12 to stop:");
roomNum = input.nextInt();
System.out.println("Enter name for room " roomNum " :");
roomName = input.next();
有問題的原因有很多……
- 如果用戶不輸入
int值并且... nextInt將在輸入流中留下一個懸空的新行,這可能會干擾稍后對輸入的決議next不會考慮用戶是否輸入firstname lastname,它只會拾取firstname,這意味著下一次執行nextInt將失敗(因為lastname仍在流中)。- 如果用戶輸入的房間號超出了可接受的范圍(包括 0-11),您只需繼續詢問名稱并嘗試更新陣列,這將導致混淆(因為我想退出)和
ArrayIndexOutOfBoundsException
作為一般建議,Scanner#nextLine請分別使用和決議String。
當您獲得房間號時,您必須先驗證它,然后再繼續詢問名稱并處理超出范圍值的可能性......
if (roomNum >= 0 && roomNum < hotel.length) {
System.out.println("Enter name for room " roomNum " :");
roomName = input.nextLine();
hotel[roomNum] = roomName;
} else if (roomNum < 0 || roomNum > 12) {
System.out.println(roomNum " is not a valid room number");
}
我還認為do-while在這些情況下回圈非常不受歡迎,您必須至少進行一次迭代,因此您不妨構造回圈,以便在最后完成退出條件(恕我直言)
可運行的示例...
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String roomName;
int roomNum = 0;
String[] hotel = new String[12];
initialise(hotel);
do {
for (int x = 0; x < hotel.length; x ) {
if (hotel[x].equals("e")) {
System.out.println("room " x " is empty");
} else {
System.out.println("room " x " is occupied by " hotel[x]);
}
}
System.out.println("Enter room number (0-11) or 12 to stop:");
String value = input.nextLine();
try {
roomNum = Integer.parseInt(value);
if (roomNum >= 0 && roomNum < hotel.length) {
System.out.println("Enter name for room " roomNum " :");
roomName = input.nextLine();
hotel[roomNum] = roomName;
} else if (roomNum < 0 || roomNum > 12) {
System.out.println(roomNum " is not a valid room number");
}
} catch (NumberFormatException exp) {
System.out.println(value " is not a valid room number");
}
System.out.println("");
} while (roomNum != 12);
}
private static void initialise(String hotelRef[]) {
for (int x = 0; x < hotelRef.length; x ) {
hotelRef[x] = "e";
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464446.html
