布尔二维数组 [英] Boolean 2-D Array

查看:123
本文介绍了布尔二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不擅长Java,目前正在学校学习。

我在这里寻求帮助,我做了一些代码但有错误。



我想创建一个布尔2-D剧院类的数组,这是我的代码:

I''m not good in Java, currently learning it in school.
I''m here for some help, i did some code but have errors.

I want to create a boolean 2-D Array for theaterclass, and this is my code:

import java.util.Scanner;
public class MovieHallOne
{
  public static void main(String[] args)
  {
      Scanner scanStr = new Scanner(System.in);
      boolean[][] seat = new boolean[5][5];
      seat[0][0] = true;
      System.out.println("X = Seat occupied"+"\n\n"+"   <<--------- SCREEN ---------- >>");
      for(int i = 0; i < 5; i++)
      {
          for(int j = 0; j < 5; j++)
          {
              String seating =(seat[i][j]==true)?"   [X] ":"   [ ] ";
              System.out.print(seating);
          }
          System.out.println("");
      }
      System.out.print("\nPlease choose a seat:");
      String chooseSeat = scanStr.nextLine();
  }
}



到目前为止,我的代码结果如下:

http://i187.photobucket.com/albums/x163/DarkChorus/TheaterHallOne.jpg [ ^ ]



问题是我希望用户选择座位,从而使影院大厅的布局改变如下:



在用户选择座位之前:


So far, the result of my codes look like this :
http://i187.photobucket.com/albums/x163/DarkChorus/TheaterHallOne.jpg[^]

The problem is I want the user to choose the seat, thus having the layout of the cinema hall to change as in :

Before user selects seat :

<br />
 X = Seat occupied<br />
	  <<------------ SCREEN ------------ >>   <br />
	A1[ ]  A2[ ]   A3[ ]  A4[ ]  A5[ ]  A6[ ]<br />
	B1[ ]  B2[ ]   B3[ ]  B4[ ]  B5[ ]  B6[ ]<br />
	C1[ ]  C2[ ]   C3[ ]  C4[ ]  C5[ ]  C6[ ]<br />
	D1[ ]  D2[ ]   D3[ ]  D4[ ]  D5[ ]  D6[ ]<br />
	E1[ ]  E2[ ]   E3[ ]  E4[ ]  E5[ ]  E6[ ]<br />
<br />
Please choose your seat:<br />



======

用户选择座位后, A3和A4,X将标记显示座位被占用的方括号:




======
And after user selects the seat, "A3" and "A4", the X will mark the square brackets showing the seat''s occupied:

<br />
 X = Seat occupied<br />
	  <<------------ SCREEN ------------ >>   <br />
	A1[ ]  A2[ ]   A3[X]  A4[X]  A5[ ]  A6[ ]<br />
	B1[ ]  B2[ ]   B3[ ]  B4[ ]  B5[ ]  B6[ ]<br />
	C1[ ]  C2[ ]   C3[ ]  C4[ ]  C5[ ]  C6[ ]<br />
	D1[ ]  D2[ ]   D3[ ]  D4[ ]  D5[ ]  D6[ ]<br />
	E1[ ]  E2[ ]   E3[ ]  E4[ ]  E5[ ]  E6[ ]<br />
<br />
Please choose your seat:





但是我的代码不是这样打印的,我已将上面的图片链接起来,显示在[]前面没有A1等等......如何让用户选择座位,它会自动将X存储到[]中?



这只是1号馆,还有2个大厅有更多的座位,所以如果我得到这个大厅,我会没事的其余的大厅。



我正在使用非常简单的方法,并没有触及GUI的东西,所以不要试着告诉我这些东西或者我不会我能够理解,提前谢谢!



任何想法,解决方案,我应该做的改进,因为我在我的智慧结束?



:((:confused ::(((



However my code does not print like this, I have linked an image above, showing that there''s no "A1" and so on in front of the "[ ]" ... and how do I enable user to select the seat and it will automatically "store" the "X" into the "[ ]"?

And this is just Hall 1, and there are 2 more halls with more seats, so if i get this hall right, I''ll be fine with the rest of the halls.

I''m using really simple methods and have not touched on GUI stuffs, so do not try telling me about those stuffs or I wouldn''t be able to understand, thanks in advance!

Any ideas, solutions, improvement I should make as I''m at my wits'' end?

:(( :confused: :((

推荐答案

你看起来在这里有你的思考上限,所以我不打算发布代码(我目前也没有Java编译器/ IDE安装),但我会给你一个面包碎屑跟踪。你应该好好的,但是如果你正在寻找更多的代码,不要害怕问。



你好像你能想到/编码你的通过这个方式。



您将不得不伪造序列中的字母。例如,我们知道 ASCII值 [ ^ ]对于大写A是65.因此,您可以使用(char)65 来打印这封信出。如果您将 i 的值添加到此,您将在后续行中获得B和C(等)。



你应该使用 String.format [ ^ ]来创建该行。这将允许您预先计算字母/数字并为数组的每个元素插入它,更重要的是,使代码易于阅读。我的意思是你应该改变构建座位变量的方式,并输出正确的字母和代表当前数组中元素的数字绘图。



最后,在输入端,你会想做一些事情:

1)验证输入在字符串长度的两个字符中

2)检查第一个字符,然后减去65.这是您将存储该值的行。

3)检查第二个字符,并使用 Integer.parseint 将值提取为整数[ ^ ]。这个值将是你的''列''。



有了这些信息,你将拥有在数组中正确元素上翻转布尔值所需的数据。



祝你好运!
You look like you''ve got your thinking cap on here, so I am not going to post code (I also don''t currently have a Java compiler/IDE installed), but I will give you a bread crumb trail to follow. You should make out okay, but if you''re looking for more code don''t be afraid to ask.

You seem like you can think/code your way through this.

You will have to pseudo-generate the letters in your sequences. For example, we know that the ASCII value[^] for capital ''A'' is 65. You can therefore use (char)65 to print the letter out. If you add the value of i to this, you will then get your B and your C (etc.) on subsequent lines.

You should use String.format[^] to create the line. This will allow you to pre-compute the letter/number and insert it for each element of the array and, more importantly, keep your code easy to read. What I mean by this is that you should change the way you build your seating variable and have it output the correct letter and the number representing the element in the array that you are currently drawing.

Finally, on the input side, you''ll want to do a few things:
1) Verify that the input in two chars with the string length
2) Check the first character, then minus 65. That is the ''row'' that you will store the value in.
3) Check the second character, and extract the value as an integer using Integer.parseint[^]. This value will be your ''column''.

With that information, you will have the data you need to flip the boolean value on the correct element in your array.

Good luck!


这是这样的吗?



有没有关于如何使用(char)65 的例子?





引用:您必须伪序生成序列中的字母。例如,我们知道大写A的ASCII值[^]是65.因此,您可以使用(char)65打印出来的字母。如果你将i的值添加到此,你将在后续行中获得你的B和你的C(等等)。



Is this the way to do it?

Are there any examples on how to use the(char)65?


Quote : You will have to pseudo-generate the letters in your sequences. For example, we know that the ASCII value[^] for capital ''A'' is 65. You can therefore use (char)65 to print the letter out. If you add the value of i to this, you will then get your B and your C (etc.) on subsequent lines.

String Alpha[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

      <big>
      int i = (char)65;
      int j = (char)65;
      </big>
      System.out.println("X = Seat occupied"+"\n\n"+"   <<--------- SCREEN ---------- >>");
      for(i = 0; i < 5; i++)
      {
          for(j = 0; j < 5; j++)
          {
             String seating =(seat[i][j]==true)?"   [X] ":"   [ ] ";
             System.out.print(seating);
          }
          System.out.println("");
      }
      System.out.print("\nPlease choose a seat:");
      String chooseSeat = scanStr.nextLine();







关于使用String.format ,是这样的:






And about using String.format, is is something like this:

String format = "what to place here..."
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");





任何改变我建立座位变量的方式的示例或代码,并打印正确的字母和代表我目前正在绘制的数组中元素的数字?



很抱歉,如果我误解或者不理解你的提示..



只需要更多面包屑或线索。

:confused:



Any examples or codes to change the way I build my seating variable and have it print the correct letter and the number representing the element in the array that I am currently drawing?

Sorry if I misunderstood or not really understood your tips ..

Just need more "bread crumbs" or clues.
:confused:


不要使用整数值来表示字符,因为它会使你的代码不那么清晰,你可以这样做一个简单的循环:

Do not use integer values to represent characters as it makes your code less clear, you can do a simple loop like this:
for (char row = ''A''; row < ''C''; row++) {
    for (int col = 1; col < 4; col++) {
    System.out.format("%c%d[ ] ", row, col);
    }
    System.out.println("");
}



添加用于检查是否打印X的代码应该不会太困难。你可以使用像


The addition of code to check whether to print the ''X'' should not be too difficult. You can use an expression like

int index = row - ''A'';

在你的数组中查找行索引。



还记得用户输入的输入最多的座位标识符可能使用小写字符,因此您应该检查字符a以及A。

to find the row index into your array.

Also remember when the user enters a seat identifier that the input will most likely use lower case characters so you should check for character ''a'' as well as ''A''.


这篇关于布尔二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆