首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在java中使用用户输入值更新数组中的数据?

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

Stack Overflow用户
提问于 2019-11-27 02:06:32
回答 2查看 929关注 0票数 0

我正在尝试创建一个三层的车库,每层有三个地块。我对java还是个新手,所以我在这方面真的遇到了麻烦。我想问用户他们是离开(0)还是停车(1),然后他们在哪个楼层或想要在哪个楼层,以及他们在哪个停车场或想要在哪个停车场。然后,我希望使用该数据并更新数组,以显示为该位置保留的数据。但是我有的东西不起作用。任何帮助都将不胜感激。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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]+"]   ");
}
        }   
    }               
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-27 16:59:13

你已经很接近了。您需要删除重置所有停车位的部分。而且,在用户输入他们的选择之后,您可能不需要打印任何内容。

为了获得额外的积分,您需要添加错误检查。如果用户输入#12345空间怎么办?或者说“你好”?你应该处理这些案件。

以下是您的代码,稍作修改:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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;
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2019-11-27 02:27:46

您有一个冗余的if(input == 1)检查。它可能只是:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
if (input == 1 && parkingSpace[floor][lot].equals("Empty")) {

parkingSpace[floor][lot] = "Reserved";

}

同样,你的代码也适用于我。不过,我确实需要添加几个括号来关闭方法和类。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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] + "]   ");
        }
      }
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59062008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文