我正在尝试创建一个三层的车库,每层有三个地块。我对java还是个新手,所以我在这方面真的遇到了麻烦。我想问用户他们是离开(0)还是停车(1),然后他们在哪个楼层或想要在哪个楼层,以及他们在哪个停车场或想要在哪个停车场。然后,我希望使用该数据并更新数组,以显示为该位置保留的数据。但是我有的东西不起作用。任何帮助都将不胜感激。
import java.util.Scanner;
public class Parking {
public static void main(String[] args) {
String parkingspace[][] = new String[3][3];
for(int floor=0; floor<parkingspace[0].length; floor++) {
for(int lot=0; lot<parkingspace[floor].length; lot++) {
parkingspace[floor][lot]="Empty";
}
}
Scanner scan = new Scanner(System.in);
while(true){
for(int floor=0; floor<parkingspace[0].length; floor++) {
for(int lot=0; lot<parkingspace[floor].length; lot++) {
parkingspace[floor][lot]="Empty";
System.out.print("Floor "+floor + ": Lot #" +lot +": [" + parkingspace[floor][lot]+"] ");
}
System.out.println();
}
System.out.println("Are you leaving(0) or parking(1)?");
int input = scan.nextInt();
System.out.println("Which floor (0, 1, 2)?");
int floor = scan.nextInt();
System.out.println("Which lot (0, 1, 2)?");
int lot = scan.nextInt();
if(input==1) {
if(parkingspace[floor][lot].equals("Empty")) {
if(input==1) {
parkingspace[floor][lot]="Reserved";
System.out.print("Floor "+ floor + ": Lot #" +lot +": [" + parkingspace[floor][lot]+"] ");
}
}else if(input==0){
parkingspace[floor][lot]="Empty";
System.out.print("Floor "+ floor + ": Lot #" +lot +": [" + parkingspace[floor][lot]+"] ");
}
}
}
}
发布于 2019-11-27 16:59:13
你已经很接近了。您需要删除重置所有停车位的部分。而且,在用户输入他们的选择之后,您可能不需要打印任何内容。
为了获得额外的积分,您需要添加错误检查。如果用户输入#12345空间怎么办?或者说“你好”?你应该处理这些案件。
以下是您的代码,稍作修改:
public static void main(String[]args) {
String parkingspace[][] = new String[3][3];
// Make these variables to be consistent
final String empty = "EMPTY";
final String reserved = "RSRVD";
// Initialize
for (int floor = 0; floor < parkingspace.length; floor++) {
for (int lot = 0; lot < parkingspace[floor].length; lot++) {
parkingspace[floor][lot] = empty;
}
}
Scanner scan = new Scanner(System.in);
while (true) {
// Print the parking lot
for (int floor = 0; floor < parkingspace.length; floor++) {
System.out.print("Floor " + floor + ": ");
for (int lot = 0; lot < parkingspace[floor].length; lot++) {
System.out.print("Lot #" + lot + ": [" + parkingspace[floor][lot] + "] ");
}
System.out.println();
}
// Get user input
System.out.println("Are you leaving(0) or parking(1)?");
int input = scan.nextInt();
System.out.println("Which floor (0, 1, 2)?");
int floor = scan.nextInt();
System.out.println("Which lot (0, 1, 2)?");
int lot = scan.nextInt();
// Update parking lot
if (input == 1 && parkingspace[floor][lot].equals(empty)) {
parkingspace[floor][lot] = reserved;
}
else if (input == 0 && parkingspace[floor][lot].equals(reserved)) {
parkingspace[floor][lot] = empty;
}
}
}
发布于 2019-11-27 02:27:46
您有一个冗余的if(input == 1)
检查。它可能只是:
if (input == 1 && parkingSpace[floor][lot].equals("Empty")) {
parkingSpace[floor][lot] = "Reserved";
}
同样,你的代码也适用于我。不过,我确实需要添加几个括号来关闭方法和类。
import java.util.Scanner;
public class Parking {
public static void main(String[] args) {
String parkingspace[][] = new String[3][3];
for (int floor = 0; floor < parkingspace[0].length; floor++) {
for (int lot = 0; lot < parkingspace[floor].length; lot++) {
parkingspace[floor][lot] = "Empty";
}
}
Scanner scan = new Scanner(System.in);
while (true) {
for (int floor = 0; floor < parkingspace[0].length; floor++) {
for (int lot = 0; lot < parkingspace[floor].length; lot++) {
parkingspace[floor][lot] = "Empty";
System.out.print(
"Floor " + floor + ": Lot #" + lot + ": [" + parkingspace[floor][lot] + "] ");
}
System.out.println();
}
System.out.println("Are you leaving(0) or parking(1)?");
int input = scan.nextInt();
System.out.println("Which floor (0, 1, 2)?");
int floor = scan.nextInt();
System.out.println("Which lot (0, 1, 2)?");
int lot = scan.nextInt();
if (input == 1) {
if (parkingspace[floor][lot].equals("Empty")) {
if (input == 1) {
parkingspace[floor][lot] = "Reserved";
System.out.print(
"Floor " + floor + ": Lot #" + lot + ": [" + parkingspace[floor][lot] + "] ");
}
} else if (input == 0) {
parkingspace[floor][lot] = "Empty";
System.out.print(
"Floor " + floor + ": Lot #" + lot + ": [" + parkingspace[floor][lot] + "] ");
}
}
}
}
}
https://stackoverflow.com/questions/59062008
复制相似问题