帮助使用二维数组的 Java Sudoku Permuter 程序?
我必须创建一个程序,将数独的 9 行显示为 9 个 9 位数字,然后提示用户对数独执行 6 种操作之一。然后我们必须在用户每次执行操作时输出数独。这是应该如何进行的示例运行:
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:
假设用户输入 3(交换两行面板)。这将出现:
Enter row panels (1-3) to swap: 3 1
它将交换行面板 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.
假设用户输入 5。这将出现:
Enter two numbers: 2 8
原始数独再次输出,除了 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
如果用户输入 1,就会出现一些内容,
Enter two rows (1-9) to switch:
无论用户输入哪一行,这两行都会被交换,并且数独将再次被输出。如果用户输入 2,情况会类似,只不过 2 列会被切换。同样,如果用户输入4,则两个列面板将被切换。
我们应该使用这样的二维数组:
int [] [] sudoku = new int[10] [10]
我不知道如何做到这一点。我整个学期都在挣扎,这是我的第一堂编程课。我根本不理解数组,而且我不明白我们应该如何显示数独。这个问题不在我们的书中,所以我也没有什么可回顾的。我真的需要通过这门课。如果有人可以帮助我,我真的很感激。尽量让它易于理解,有很多东西我还没有学会如何做(例如:郑重声明,我不知道 parseInt 是什么)。我尝试过读这本书(好几次)。它对一些人有帮助,但这个计划将是不可能的。非常感谢您的帮助。
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. 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). 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:
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:
Enter two numbers: 2 8
The original sudoku is outputted again, except 2's and 8's are switched throughout.
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
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. Similarly, if the user entered 4, two column panels would be switched.
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). 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
例如,下面的代码块仅打印出数组的原始内容。这段代码有意义吗?
以下是如何对示例板进行硬编码以使用它:
以下是一些用于交换两行的伪代码。
交换基本上需要一个临时变量来在交换时保存其中一个值:
For instance, here's a block of code that just prints out the raw contents of the array. Does this code make sense?
Here's how you can hard-code your sample board to work with it:
Here's some pseudo-code for swapping two rows.
Swapping basically requires a temporary variable to hold one of the values while swapping:
正如 mellamokb 的评论所说,将事物分解成多个部分就是诀窍。乍一看,我可能会做这样的事情:
一旦你开始工作,你实际上就快完成了。您可以从 #3 中获取您的例程,并对其进行一些小的更改,以进行其他 4 项修改。
您没有提及数独板的来源。如果它是硬编码的,那么你就完成了。如果没有,那么您所要做的就是制定该方法。此时,您已经知道可以正确打印板、显示菜单并更换板。
你提到了数组的问题,它们可能是一个飞跃。您是否有关于数组的具体问题需要我们帮助解决?它们就像编程中的其他东西一样。你开始对它们不太了解,只是遵循你在其他地方找到的指导和代码。随着你获得更多的经验(正如你在这个项目和未来的项目中一样),它们将不再那么神秘,并且变得更有意义,直到有一天它们像 3 + 7 一样简单。
As mellamokb's comment says, breaking things down into parts is the trick. At first glance I would probably do something like this:
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.
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.