简体   繁体   English

帮助使用二维数组的Java Sudoku Permuter程序?

[英]Help With Java Sudoku Permuter Program using Two Dimensional Array?

I have to create a program that displays the 9 rows of a sudoku as 9 9-digit numbers and then prompt the user to do one of 6 operations on the sudoku. 我必须创建一个程序,将数独的9行显示为9个9位数字,然后提示用户对数独执行6个操作之一。 Then we have to output the sudoku each time the user performs an operation. 然后,每次用户执行操作时,我们都必须输出数独。 This is sort of a sample run of how it should go: 这是运行方式的一个示例运行:

Welcome to Sudoku Permuter.

   C C C C C C C C C
   1 2 3 4 5 6 7 8 9
R1 0 8 0 4 0 2 0 6 0
R2 0 3 4 0 0 0 9 1 0
R3 9 6 0 0 0 0 0 8 4
R4 0 0 0 2 1 6 0 0 0
R5 2 0 0 0 0 9 6 0 0
R6 0 1 0 3 5 7 0 0 8
R7 8 4 0 0 0 0 0 7 5
R8 0 2 6 0 0 0 1 3 0
R9 0 9 0 7 0 1 0 4 0

(0 denotes a blank)

Enter 1 to swap two rows in a panel
Enter 2 to swap two columns in a panel
Enter 3 to swap two row panels
Enter 4 to swap two column panels
Enter 5 to swap two numbers
Enter 0 to end:

Let's say the user enters 3 (to swap two row panels). 假设用户输入3(以交换两行面板)。 This would come up: 这会出现:

Enter row panels (1-3) to swap: 3 1

It would swap row panels 1 and 3, and this would be the output: 它将交换行面板1和3,这将是输出:

   C C C C C C C C C
   1 2 3 4 5 6 7 8 9
R1 8 4 0 0 0 0 0 7 5
R2 0 2 6 0 0 0 1 3 0
R3 0 9 0 7 0 1 0 4 0
R4 0 0 0 2 1 6 0 0 0
R5 2 0 0 0 0 9 6 0 0
R6 0 1 0 3 5 7 0 0 8
R7 0 8 0 4 0 2 0 6 0
R8 0 3 4 0 0 0 9 1 0
R9 9 6 0 0 0 0 0 8 4

Rows 1-3 have been switched with rows 7-9.

Let's say the user inputs 5. This comes up: 假设用户输入了5。出现了:

Enter two numbers: 2 8

The original sudoku is outputted again, except 2's and 8's are switched throughout. 再次输出原始的数独,但始终切换了2和8。

   C C C C C C C C C
   1 2 3 4 5 6 7 8 9
R1 0 2 0 4 0 8 0 6 0
R2 0 3 4 0 0 0 9 1 0
R3 9 6 0 0 0 0 0 2 4
R4 0 0 0 8 1 6 0 0 0
R5 8 0 0 0 0 9 6 0 0
R6 0 1 0 3 5 7 0 0 2
R7 2 4 0 0 0 0 0 7 5
R8 0 8 6 0 0 0 1 3 0
R9 0 9 0 7 0 1 0 4 0

If the user entered 1, something would come up saying 如果用户输入1,将会出现一些提示

Enter two rows (1-9) to switch:

And whichever rows the user enters, those two individual rows would be swapped and the sudoku would once again be outputted. 无论用户输入哪一行,这两个单独的行都将被交换,数独将再次输出。 It'd be similar if the user entered 2, except 2 columns would be switched. 如果用户输入2,则将类似,但将切换2列。 Similarly, if the user entered 4, two column panels would be switched. 同样,如果用户输入4,则将切换两个列面板。

We're supposed to use a two dimensional array like this: 我们应该使用这样的二维数组:

int [] [] sudoku = new int[10] [10]

I have no idea how to do this. 我不知道该怎么做。 I've been struggling all semester, this is my first programming class. 我整个学期都在努力,这是我的第一门编程课。 I just don't understand arrays at all, and I don't understand how we're even supposed to display the sudoku in the first place. 我只是根本不了解数组,也不了解我们应该如何首先显示数独。 This problem isn't in our book, so I have nothing to look back on, either. 这个问题不在我们的书中,因此,我也无需回顾。 I really need to pass this class. 我真的需要通过这堂课。 If anyone could help me, I really appreciate it. 如果有人可以帮助我,我真的很感激。 Try to make it easy to understand, there's a lot of stuff I haven't learned how to do yet (ex: for the record, idk what parseInt is). 尝试使其易于理解,有很多东西我还没学会怎么做(例如:记录下来,idk是parseInt是什么)。 I've tried reading the book (several times). 我试图读过这本书(几次)。 It helps some, but this program is going to be impossible. 它可以帮助一些人,但是这个程序将是不可能的。 Thank you SO much for the help. 非常感谢你的帮助。

  1. Do you know how to read input? 你知道如何阅读输入吗?
  2. Do you know how to work with 1-dimensional arrays (lists), like {1, 2, 3, 4, 5}? 您知道如何使用一维数组(列表),例如{1、2、3、4、5}吗?
  3. Do you understand for loops? 您了解循环吗?
  4. What part of the concept of arrays seems difficult to you? 数组概念的哪一部分对您来说似乎很困难?

For instance, here's a block of code that just prints out the raw contents of the array. 例如,下面的代码块仅打印出数组的原始内容。 Does this code make sense? 这段代码有意义吗?

int [] [] sudoku = new int[10] [10];
// loop through all of the rows
for (int row = 0; row < 10; row++) {
    // loop through all columns for each row
    for (int column = 0; column < 10; column++) {
        // print out the sudoku value at that row and column
        System.out.print(sudoku[row] [column] + " ");
    }
    // at the end of the row, print a blank line to start the next row
    System.out.println();
}

Here's how you can hard-code your sample board to work with it: 您可以通过以下方式对示例板进行硬编码以使用它:

int [] [] sudoku = new int [] [] {
    { 0, 8, 0, 4, 0, 2, 0, 6, 0 },
    { 0, 3, 4, 0, 0, 0, 9, 1, 0 },
    etc.
}

Here's some pseudo-code for swapping two rows. 这是一些用于交换两行的伪代码。

Get first row # from user
Get second row # from user
Loop through each column in the board
    Swap(cell at first row #, current column, cell at second row #, current column)
End Loop

Swapping basically requires a temporary variable to hold one of the values while swapping: 交换基本上需要一个临时变量来在交换时保存其中一个值:

Swap(a, b)
    Store a into Temp
    Store b into a
    Store Temp into b

As mellamokb's comment says, breaking things down into parts is the trick. 正如mellamokb的评论所言,把事情分解成几部分是诀窍。 At first glance I would probably do something like this: 乍一看,我可能会做这样的事情:

  1. Hardcode in a sudoku board, and make a routine to print the board out 在数独板中进行硬编码,并制作例程以将板打印出来
  2. Once that works right, then you'd need a menu. 一旦工作正常,那么您将需要一个菜单​​。 So extend your program to make a little menu that prints back the user's selection, and does nothing more 因此,扩展程序以创建一个小菜单,该菜单会打印出用户的选择,并且仅执行其他操作
  3. Once that's going, you can start filling in the menu choices. 完成后,您可以开始填写菜单选项。 So when the user selects option one, make the routine for swapping two rows. 因此,当用户选择选项一时,使例程交换两行。 The other menu items can still just print out their number. 其他菜单项仍可以仅打印其编号。

Once you've got that working, you're actually nearly done. 一旦完成工作,您实际上就快完成了。 You can take your routine from #3 and make copies of it with small changes to make other 4 modifications possible. 您可以从#3中提取例程,并对其进行细微的复制,以使其他4种修改成为可能。

You don't mention where the sudoku board comes from. 您没有提到数独板的来源。 If it's hardcoded, you're done. 如果它是硬编码的,那么就完成了。 If not, then all you have to is make that method. 如果没有,那么您要做的就是使用该方法。 At this point, you already know that you can print the board right, show the menu, and change the board. 至此,您已经知道可以正确打印板,显示菜单并更换板了。

You mention problems with arrays, and they can be a bit of a leap. 您提到了数组问题,它们可能会有些飞跃。 Is there a specific question you have about arrays that we might be able to help with? 关于阵列,您是否有特定的问题可以提供帮助? They are like anything else in programming. 它们就像编程中的其他任何东西。 You start not knowing a ton about them, and just sort of following what guidance and code you find elsewhere. 您开始不了解它们,只是遵循您在其他地方找到的指导和代码。 As you gain more experience (as you will on this project and future projects), they'll be less mysterious and make more sense until one day they're as easy as 3 + 7. 随着您获得更多的经验(就像您将在此项目和将来的项目中所做的那样),它们将变得不那么神秘,并且变得更加有意义,直到有一天它们变得像3 + 7一样容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM