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

发布于 2024-10-02 14:42:33 字数 1844 浏览 8 评论 0原文

我必须创建一个程序,将数独的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-10-09 14:42:33
  1. 你知道如何读取输入吗?
  2. 您知道如何使用一维数组(列表),例如 {1, 2, 3, 4, 5}?
  3. 你了解for循环吗?
  4. 数组概念的哪一部分对您来说似乎很难?

例如,下面的代码块仅打印出数组的原始内容。这段代码有意义吗?

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();
}

以下是如何对示例板进行硬编码以使用它:

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

以下是一些用于交换两行的伪代码。

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

交换基本上需要一个临时变量来在交换时保存其中一个值:

Swap(a, b)
    Store a into Temp
    Store b into a
    Store Temp into b
  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}?
  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
又爬满兰若 2024-10-09 14:42:33

正如 mellamokb 的评论所说,将事物分解成多个部分就是诀窍。乍一看,我可能会做这样的事情:

  1. 在数独板上进行硬编码,并制定一个例程来打印板子
  2. 一旦工作正常,那么您就需要一个菜单​​。因此,扩展您的程序以制作一个小菜单,打印回用户的选择,并且不执行任何其他操作。
  3. 一旦完成,您就可以开始填写菜单选项。因此,当用户选择选项一时,制定交换两行的例程。其他菜单项仍然可以打印出它们的编号。

一旦你开始工作,你实际上就快完成了。您可以从 #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:

  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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文