gpt4 book ai didi

java - 为什么我的程序中存储的值总是被删除

转载 作者:行者123 更新时间:2023-12-02 07:53:52 25 4
gpt4 key购买 nike

好吧,我正在尝试完成数据结构的简单项目,但总是有一些错误不断出现在我的程序中。我已经修复了其中一些,但有一个让我想放弃。也许你们可以帮我找到解决方案?

这是我的代码(LogInSystem 类):

    // NMQ

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

public class LogInSystem {
private static final int MAX_SEATS = 10;

Vector<String> username = new Vector<String>();
ArrayList<String> password = new ArrayList<String>();

int p;
String Username, Password, rUsername, rPassword;

private Scanner input = new Scanner(System.in);


public LogInSystem() {
loginScreen();
}
private void loginScreen() {
boolean done = false;

do {
printMainMenu();
int choice = getMainMenuChoice();

switch (choice) {
case 1: // Log In
logIn();
break;

case 2: // To Register
register();
break;

case 3: // Exit
done = true;
break;
}

} while (!done);
}

//Registration

private void register() {
System.out.println("Input a Username");
Username = input.next();
System.out.println("Input a Password");
Password = input.next();
System.out.println("... Registered!");

}

//Log In

private void logIn() {
p = 0;
System.out.println("Input a Username");
rUsername = input.next();
System.out.println("Input a Password");
rPassword = input.next();

// If the log in is successful it will got to the next menu

if(rUsername.equals(Username)&& rPassword.equals(Password))
{
System.out.println("... Logging In");
boolean done = false;

String[] seats = new String[MAX_SEATS]; // the item list
initializeItems(seats);
// Printing of 2nd menu
do {
printMainMenu2();
int choice = getMainMenuChoice2(); // choice off of the main menu

switch (choice) {
case 1: // Adding Seat
addSeat(seats);
break;
case 2: // Viewing Seat List
viewSeatList(seats);
break;
case 3: // Exit
done = true;
break;
}

} while (!done);
}

else
System.out.println("Invalid log in, please try again.");

}
private int getMainMenuChoice() {
int choice; // choice entered
boolean valid = false; // is choice valid?

do {
System.out.print(">>>>> ");
choice = cineplexError.readInt();

if (1 <= choice && choice <= 4) {
valid = true;
} else {
System.out.println("Invalid choice.");
}
} while (!valid);

return choice;
}
private void printMainMenu() {
System.out.println("\nMain Menu\n");
System.out.println("Press 1 to Log In");
System.out.println("Press 2 to Register");
System.out.println("Press 3 to Exit");
System.out.println();
}
public static void main(String[] args) {
new LogInSystem();
}

int getMainMenuChoice2() {
int choice; // choice entered
boolean valid = false; // is choice valid?

do {
System.out.print(">>>>> ");
choice = cineplexError.readInt();

if (1 <= choice && choice <= 4) {
valid = true;
} else {
System.out.println("Invalid choice.");
}
} while (!valid);

return choice;
}
void printMainMenu2() {
System.out.println("\nMain Menu\n");
System.out.println("Press 1 to Reserve a Seat");
System.out.println("Press 2 to View the Seat List");
System.out.println("Press 3 to Exit Buying");
System.out.println();
}
void initializeItems(String[] seats) {
for (int i = 0; i < seats.length; i++) {
seats[i] = "";
}
}
void addSeat(String[] seats) {
int seatIndex = findEmptySeatList(seats); // index of first empty item list
if (seatIndex == seats.length) {
System.out.println("Seat is already occupied. Sorry.");
}

else {
String seat = getSeatName(); // seat's name
seats[seatIndex] = seat;
System.out.println(seat + " is on seat list #"
+ (seatIndex + 1));
}
}
int findEmptySeatList(String[] seat) {
for (int i = 0; i < seat.length; i++) {
if (isEmpty(seat, i)) {
return i;
}
}

return seat.length;
}
boolean isEmpty(String[] seats, int seatIndex) {
return seats[seatIndex].equals("");
}
String getSeatName() {
System.out.print("Enter the name of reservator: ");
return cineplexError.readString();
}
void viewSeatList(String[] seats) {
System.out.println("\nSeat List\n");
for (int i = 0; i < seats.length; i++) {
System.out.println((i + 1) + ". " + seats[i]);
}
}
}

这是另一个类(cineplexError):

// NMQ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class cineplexError {
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
private static String integerReprompt = "Invalid integer. Try again: ";
private static String doubleReprompt = "Invalid double. Try again: ";
private static String charReprompt = "Invalid character. Try again: ";

public static String readString() {
String s = null;
try {
s = in.readLine();
} catch (IOException ex) {}
return s;
}

public static char readChar() {
char c = 0;
String s = null;
try {
s = in.readLine();
} catch (IOException ex) {}
if (s.length() == 1) {
c = s.charAt(0);
// valid = true;
} else {
System.out.print(charReprompt);
}
return c;
}

public static int readInt() {
int i = 0;

boolean valid = false;

try {
i = Integer.parseInt(in.readLine());
} catch (IOException ex) {
System.out.print(integerReprompt);
}
valid = true;



return i;
}

public static double readDouble() {
double d = 0.0;

boolean valid = true;

try {
d = Double.parseDouble(in.readLine());
} catch (IOException ex) {}
valid = true;
valid = false;
System.out.print(doubleReprompt);


return d;
}

public void pause() {
System.out.print("Press enter to continue...");
try {
in.readLine();
} catch (IOException ex) {}
}

public static void setIntegerReprompt(String prompt) {
integerReprompt = prompt;
}

public void setDoubleReprompt(String prompt) {
doubleReprompt = prompt;
}

public void setCharReprompt(String prompt) {
charReprompt = prompt;
}

}
<小时/>

程序中的错误是,每当我预订座位并注销并重新登录时,当我查看座位列表时,预订者的姓名或预订座位的人的姓名都会被删除。我应该放置一个控制语句来永久存储该值吗?你能帮我吗?谢谢。

最佳答案

Line 73:   String[] seats = new String[MAX_SEATS]; // the item list
Line 74: initializeItems(seats);

每次调用 logIn 时,您都会将席位列表重新初始化为所有空字符串。该初始化应该在构造函数中完成。因此,将第 73 行(String[] 席位的声明)移至该类的实例变量,并将第 74 行移至构造函数中。

关于java - 为什么我的程序中存储的值总是被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9900644/

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