如何在使用单独的过程时将数据存储到数组中?
How can I store data into an array whilst using a seperate procedure?
我一直在努力让我的程序按照我想要的方式运行。下面这段代码不能完全工作,我不明白为什么,我也需要它作为一个过程,但我不知道如何解决这个问题。
这是我需要的代码作为一个单独的程序,它允许用户 select 数组中的哪个房间并存储用户在 'A' 中输入后要输入的名称菜单:
System.out.println("Enter room number(0-9)");
roomNum =input.nextInt();
System.out.println("Enter name for room " + roomNum + ": " ) ;
Client = input.next();
hotel[roomNum] = Client;
add(hotel, Client);
System.out.println(" ");
这是我的完整程序:
package hotel_program;
import java.util.*;
public class Hotel_Program {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String roomID;
String[] hotel = new String[10];
int roomNum = 0;
char selection;
boolean run = true;
String Client;
for(int i=0;i<hotel.length;i++)
{
hotel[i] = "e";
}
// Displays the menu
while (run) {
System.out.println("---- HOTEL MENU ----");
System.out.println("[E] Display Empty Rooms");
System.out.println("[A] Add Customer");
System.out.println("[V] View All Rooms");
System.out.println("[D] Delete Customer From Room");
System.out.println("[F] Find Room For Customer Name");
System.out.println("[S] Save Data Input");
System.out.println("[L] Load Data");
System.out.println("[O] View Rooms");
System.out.println("--------------------");
selection = getInput();
switch (selection) {
// Shows rooms that are empty (not occupied)
case 'E':
for (int x = 0; x < hotel.length; x++)
{
if (hotel[x].equals("e") || hotel[x].equals("E"))
{
System.out.println("room " + x + " is empty");
}
}
break;
// Allows user to add in client name and assign to room number
case 'A':
System.out.println("Enter room number(0-9)");
roomNum =input.nextInt();
System.out.println("Enter name for room " + roomNum + ": " ) ;
Client = input.next();
hotel[roomNum] = Client;
add(hotel, Client);
System.out.println(" ");
break;
case 'V':
break;
case 'D':
break;
case 'F':
break;
case 'S':
break;
case 'L':
break;
case 'O':
break;
}
}
}
private static char getInput()
{
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
return input.charAt(0);
}
private static void initialise(String hotelRef[])
{
for (int x = 0; x < 10; x++) {
hotelRef[x] = "e";
}
System.out.println("initilise ");
}
}
项目背景:
使用单独的程序生成酒店预订系统并通过参考。
我想完成的事情:
- 允许程序接受输入 'A' 以允许用户使用设置数组将名称添加到房间号。
- 允许程序从指定房间中删除名称并清除数组中房间的索引
这是我无法解决的将数据(名称)输入数组的问题,因为我不完全知道错误的含义:
Image wit error
有了多种选择,你可以把你需要模拟的东西拆开,在你的主环境之外检查它,然后把它放回主项目。这就是我所做的。
在下面可以找到一个小程序来模拟对数组的添加和删除,从而解决您的问题。当然它不能处理某些情况,但作为 "basic program" 它允许从数组中添加和删除用户名。
import java.util.Scanner;
public class HotelBooking {
Scanner in;
String userInput;
String userName;
String[] hotelList;
int i, progressiveNumber, repeat;
HotelBooking() {
progressiveNumber = 0; // fills the array or removes from it
repeat = 0; // repeats this loop to check if it works
hotelList = new String[10]; // creates a 10 nr spaced array
while (repeat < 5) { // repeats 5 times to check if it is working
in = new Scanner(System.in);
System.out.print("Enter choice (in this case a to add or b to remove): ");
userInput = in.next();
addToArray(); // add names to array. It does not handle if user from begining choses b. it is just a demo afterall
removeFromArray(); // removes from array
}
}
private void addToArray() {
// METHOD TO ADD STRING TO ARRAY
if (userInput.equals("a")) {// handles only a. You could add "A" or whatever
System.out.print("Enter name to insert: ");
userName = in.next();
for (i = 0; i < hotelList.length; i++) {
hotelList[progressiveNumber] = userName;
}
progressiveNumber++;
System.out.print("Printing the hotel list: ");
for (String scan : hotelList) {
System.out.print(scan + " ");
}
System.out.println();
repeat++;
}
}
private void removeFromArray() {
// METHOD TO REMOVE STRING FROM ARRAY
if (userInput.equals("b")) { // handles only b. You could add "B" or whatever
System.out.print("Enter name to remove: ");
userName = "";
progressiveNumber--;
for (i = 0; i < hotelList.length; i++) {
hotelList[progressiveNumber] = userName;
}
System.out.print("Printing the hotel list: ");
for (String scan : hotelList) {
System.out.print(scan + " ");
}
System.out.println();
repeat++;
}
}
public static void main(String[] args) {
new HotelBooking();
}
}
我一直在努力让我的程序按照我想要的方式运行。下面这段代码不能完全工作,我不明白为什么,我也需要它作为一个过程,但我不知道如何解决这个问题。
这是我需要的代码作为一个单独的程序,它允许用户 select 数组中的哪个房间并存储用户在 'A' 中输入后要输入的名称菜单:
System.out.println("Enter room number(0-9)");
roomNum =input.nextInt();
System.out.println("Enter name for room " + roomNum + ": " ) ;
Client = input.next();
hotel[roomNum] = Client;
add(hotel, Client);
System.out.println(" ");
这是我的完整程序:
package hotel_program;
import java.util.*;
public class Hotel_Program {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String roomID;
String[] hotel = new String[10];
int roomNum = 0;
char selection;
boolean run = true;
String Client;
for(int i=0;i<hotel.length;i++)
{
hotel[i] = "e";
}
// Displays the menu
while (run) {
System.out.println("---- HOTEL MENU ----");
System.out.println("[E] Display Empty Rooms");
System.out.println("[A] Add Customer");
System.out.println("[V] View All Rooms");
System.out.println("[D] Delete Customer From Room");
System.out.println("[F] Find Room For Customer Name");
System.out.println("[S] Save Data Input");
System.out.println("[L] Load Data");
System.out.println("[O] View Rooms");
System.out.println("--------------------");
selection = getInput();
switch (selection) {
// Shows rooms that are empty (not occupied)
case 'E':
for (int x = 0; x < hotel.length; x++)
{
if (hotel[x].equals("e") || hotel[x].equals("E"))
{
System.out.println("room " + x + " is empty");
}
}
break;
// Allows user to add in client name and assign to room number
case 'A':
System.out.println("Enter room number(0-9)");
roomNum =input.nextInt();
System.out.println("Enter name for room " + roomNum + ": " ) ;
Client = input.next();
hotel[roomNum] = Client;
add(hotel, Client);
System.out.println(" ");
break;
case 'V':
break;
case 'D':
break;
case 'F':
break;
case 'S':
break;
case 'L':
break;
case 'O':
break;
}
}
}
private static char getInput()
{
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
return input.charAt(0);
}
private static void initialise(String hotelRef[])
{
for (int x = 0; x < 10; x++) {
hotelRef[x] = "e";
}
System.out.println("initilise ");
}
}
项目背景: 使用单独的程序生成酒店预订系统并通过参考。
我想完成的事情:
- 允许程序接受输入 'A' 以允许用户使用设置数组将名称添加到房间号。
- 允许程序从指定房间中删除名称并清除数组中房间的索引
这是我无法解决的将数据(名称)输入数组的问题,因为我不完全知道错误的含义: Image wit error
有了多种选择,你可以把你需要模拟的东西拆开,在你的主环境之外检查它,然后把它放回主项目。这就是我所做的。
在下面可以找到一个小程序来模拟对数组的添加和删除,从而解决您的问题。当然它不能处理某些情况,但作为 "basic program" 它允许从数组中添加和删除用户名。
import java.util.Scanner;
public class HotelBooking {
Scanner in;
String userInput;
String userName;
String[] hotelList;
int i, progressiveNumber, repeat;
HotelBooking() {
progressiveNumber = 0; // fills the array or removes from it
repeat = 0; // repeats this loop to check if it works
hotelList = new String[10]; // creates a 10 nr spaced array
while (repeat < 5) { // repeats 5 times to check if it is working
in = new Scanner(System.in);
System.out.print("Enter choice (in this case a to add or b to remove): ");
userInput = in.next();
addToArray(); // add names to array. It does not handle if user from begining choses b. it is just a demo afterall
removeFromArray(); // removes from array
}
}
private void addToArray() {
// METHOD TO ADD STRING TO ARRAY
if (userInput.equals("a")) {// handles only a. You could add "A" or whatever
System.out.print("Enter name to insert: ");
userName = in.next();
for (i = 0; i < hotelList.length; i++) {
hotelList[progressiveNumber] = userName;
}
progressiveNumber++;
System.out.print("Printing the hotel list: ");
for (String scan : hotelList) {
System.out.print(scan + " ");
}
System.out.println();
repeat++;
}
}
private void removeFromArray() {
// METHOD TO REMOVE STRING FROM ARRAY
if (userInput.equals("b")) { // handles only b. You could add "B" or whatever
System.out.print("Enter name to remove: ");
userName = "";
progressiveNumber--;
for (i = 0; i < hotelList.length; i++) {
hotelList[progressiveNumber] = userName;
}
System.out.print("Printing the hotel list: ");
for (String scan : hotelList) {
System.out.print(scan + " ");
}
System.out.println();
repeat++;
}
}
public static void main(String[] args) {
new HotelBooking();
}
}