Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java使用用户输入值更新数组中的数据?_Java_Arrays_Multidimensional Array_Java.util.scanner - Fatal编程技术网

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

如何使用java使用用户输入值更新数组中的数据?,java,arrays,multidimensional-array,java.util.scanner,Java,Arrays,Multidimensional Array,Java.util.scanner,我正在尝试创建一个车库,有三层,每层有三块地。我对java相当陌生,所以我在这方面真的有困难。我想问用户他们是离开(0)还是停车场(1),然后问他们在哪一层或想在哪一层,以及他们在哪一个停车场或想在哪一个停车场。然后我想使用该数据并更新数组,以显示为该点保留的数据。但我所拥有的却不起作用。任何帮助都将不胜感激 import java.util.Scanner; public class Parking { public static void main(String[] args) {

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

import java.util.Scanner;
公共停车场{
公共静态void main(字符串[]args){
字符串parkingspace[][]=新字符串[3][3];

对于(int floor=0;floor您有一个冗余的
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] + "]   ");
        }
      }
    }
  }
}
import java.util.Scanner;
公共停车场{
公共静态void main(字符串[]args){
字符串parkingspace[][]=新字符串[3][3];
对于(int floor=0;floor
您已接近。您需要删除重置所有停车位的部分。此外,在用户输入选择后,您可能不需要打印任何内容

为了获得额外的积分,您需要添加错误检查。如果用户输入空格#12345?或“Hello”?怎么办?您应该处理这些情况

这是您的代码,稍作修改:

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;
        }
    }
}
publicstaticvoidmain(字符串[]args){
字符串parkingspace[][]=新字符串[3][3];
//使这些变量保持一致
最后一个字符串empty=“empty”;
保留最终字符串=“RSRVD”;
//初始化
对于(int floor=0;地板
中的
循环的第一个
中,当
时,您重置
停车空间[floor][lot]=“空”;
重置所有内容并撤消所有其他更改。我认为您不希望这样。您的初始
floor@JohnnyMopp不,我不想那样。但是如果我想把什么东西拿出来,其他东西就不行了。有什么建议吗?我已经玩了五天了,快把我逼疯了。好吧,我把它修好了。我真的y注释掉了第一个输入==1,但当我复制并粘贴它来提问时,出于某种原因,它去掉了//。我还将其更改为if(输入==1&&parkingSpace[floor][lot]。等于(“空”))但它对我仍然不起作用。它只是在数组之前添加我输入的任何楼层和地块。它实际上并没有替换任何东西。哦,哇,我比我想象的要近得多。我想我需要在所有事情之后再次打印数组,但这一定是为什么它只是在输入数组之前添加数据现在我可以看到,由于while循环,我不需要再次打印数组,所以它只需重新打印while之后更新的数组(true).这一切现在都有意义了。此外,我还想如果他们输入的内容与指定内容不同,我应该添加一些错误消息。我现在会处理这些。非常感谢您的帮助!!!