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

问题描述 投票:-1回答:2

我正在尝试创建一个三层楼,每层三楼的车库。我对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]+"]   ");
}
        }   
    }               
}

java arrays multidimensional-array java.util.scanner
2个回答
0
投票

您有多余的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] + "]   ");
        }
      }
    }
  }
}

0
投票

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

要获得额外的信用,您需要添加错误检查。如果用户输入空间#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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.