Note that there are some explanatory texts on larger screens.

plurals
  1. POSudoku Permuter Program Help!! Two Dimensional Arrays (Java?!)
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/4207748/help-with-java-sudoku-permuter-program-using-two-dimensional-array">Help With Java Sudoku Permuter Program using Two Dimensional Array?</a> </p> </blockquote> <p>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:</p> <pre><code>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: </code></pre> <p>Let's say the user enters 3 (to swap two row panels). This would come up:</p> <p>Enter row panels (1-3) to swap: 3 1 It would swap row panels 1 and 3, and this would be the output:</p> <pre><code> 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 </code></pre> <p>Rows 1-3 have been switched with rows 7-9. Let's say the user inputs 5. This comes up:</p> <p>Enter two numbers: 2 8 The original sudoku is outputted again, except 2's and 8's are switched throughout.</p> <pre><code> 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 </code></pre> <p>If the user entered 1, something would come up saying</p> <p>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.</p> <p>We're supposed to use a two dimensional array like this:</p> <p>int [] [] sudoku = new int[10] [10]</p> <hr> <p>Ok, I'm sorry it's so long! But here's what I have so far. I'm having trouble with a few things. First, I know how to swap the rows, but since the sudoku was hard coded in, I don't know how to retrieve the numbers. I'm having trouble with arrays in general, so this program is extremely difficult. I've been working on it for at least 2 hours and this is all I have. I also have to print the sudoku before the user is prompted, but I don't know how. I tried to do System.out.println(sudoku), but random characters come up. I also don't know how to display the C1, C2, C3, etc. and the R1, R2, R3, etc. Any suggestions are appreciated! Here's my (probably terrible) code:</p> <p>import java.util.Scanner; public class SudokuPermuter { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Welcome to Sudoku Permuter.\n");</p> <pre><code> int [] [] sudoku = { { 0, 8, 0, 4, 0, 2, 0, 6, 0 }, { 0, 3, 4, 0, 0, 0, 9, 1, 0 }, { 9, 6, 0, 0, 0, 0, 0, 8, 4 }, { 0, 0, 0, 2, 1, 6, 0, 0, 0 }, { 2, 0, 0, 0, 0, 9, 6, 0, 0 }, { 0, 1, 0, 3, 5, 7, 0, 0, 8 }, { 8, 4, 0, 0, 0, 0, 0, 7, 5 }, { 0, 2, 6, 0, 0, 0, 1, 3, 0 }, { 0, 9, 0, 7, 0, 1, 0, 4, 0 } }; System.out.println(sudoku); System.out.println(); System.out.print("Enter 1 to swap two rows in a panel\n" + "Enter 2 to swap two columns in a panel\n" + "Enter 3 to swap two row panels\n" + "Enter 4 to swap two column panels\n" + "Enter 5 to swap two numbers\n" + "Enter 0 to end: "); int choice = input.nextInt(); if (choice == 1) { System.out.print("Enter 2 rows (1-9) to swap: "); int r1 = input.nextInt(); int r2 = input.nextInt(); } else if (choice == 2) { System.out.print("Enter 2 columns (1-9) to swap: "); int c1 = input.nextInt(); int c2 = input.nextInt(); } else if (choice == 3) { System.out.print("Enter 2 row panels (1-3) to swap: "); int rp1 = input.nextInt(); int rp2 = input.nextInt(); } else if (choice == 4) { System.out.print("Enter 2 column panels (1-3) to swap: "); int cp1 = input.nextInt(); int cp2 = input.nextInt(); } else if (choice == 5) { System.out.print("Enter 2 numbers (1-9) to swap: "); int num1 = input.nextInt(); int num2 = input.nextInt(); } else if (choice == 0) { System.exit(0); } else { System.out.println("Invalid input. Try again"); } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload