gpt4 book ai didi

java - 如何使用java用用户输入值更新数组中的数据?

转载 作者:行者123 更新时间:2023-12-01 19:53:47 26 4
gpt4 key购买 nike

我正在尝试创建一个三层车库,每层三个地 block 。我对java相当陌生,所以我真的遇到了麻烦。我想询问用户他们是否要离开(0)或 parking (1),然后他们在或想要在哪一层,以及他们在或想要在哪个 parking 场。然后我想使用该数据并更新数组以显示为该位置保留。但我所拥有的不起作用。任何帮助将不胜感激。

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]+"] ");
}
}
}
}

最佳答案

你很接近。您需要删除重置所有 parking 位的部分。此外,在用户输入他们的选择后,您可能不需要打印任何内容。

为了获得额外的积分,您需要添加错误检查。如果用户输入空格 #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;
}
}
}

关于java - 如何使用java用用户输入值更新数组中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59062008/

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