Well im writing a program in java and my program had to do with Sudoku but its only 4 by 4 not 9 by 9. My program checks to see weather the rows, columns, and regions add up to 10. The output would look like this...
Enter Row 1: 3 2 4 1
Enter Row 2: 4 1 3 2
Enter Row 3: 1 4 2 3
Enter Row 4: 2 3 1 4
REG-1:GOOD
REG-2:GOOD
REG-3:GOOD
REG-4:GOOD
ROW-1:GOOD
ROW-2:GOOD
ROW-3:GOOD
ROW-4:GOOD
COL-1:GOOD
COL-2:GOOD
COL-3:GOOD
COL-4:GOOD
SUDO:VALID
My problem is the last part where it says sudo:valid. I know i need to make a variable that counts the number of things that are invalid and if that variable's value is 0 at the end of the program then i have a valid sudoku. But im not sure how to do this. Anyone know how? Heres my code
Enter Row 1: 3 2 4 1
Enter Row 2: 4 1 3 2
Enter Row 3: 1 4 2 3
Enter Row 4: 2 3 1 4
REG-1:GOOD
REG-2:GOOD
REG-3:GOOD
REG-4:GOOD
ROW-1:GOOD
ROW-2:GOOD
ROW-3:GOOD
ROW-4:GOOD
COL-1:GOOD
COL-2:GOOD
COL-3:GOOD
COL-4:GOOD
SUDO:VALID
My problem is the last part where it says sudo:valid. I know i need to make a variable that counts the number of things that are invalid and if that variable's value is 0 at the end of the program then i have a valid sudoku. But im not sure how to do this. Anyone know how? Heres my code
Code:
import java.util.Scanner; public class test { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); int a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4; System.out.print ( "Enter First Row:" ); a1 = input.nextInt(); a2 = input.nextInt(); a3 = input.nextInt(); a4 = input.nextInt(); System.out.print ( "Enter Second Row:" ); b1 = input.nextInt(); b2 = input.nextInt(); b3 = input.nextInt(); b4 = input.nextInt(); System.out.print ( "Enter Third Row:" ); c1 = input.nextInt(); c2 = input.nextInt(); c3 = input.nextInt(); c4 = input.nextInt(); System.out.print ( "Enter Fourth Row:" ); d1 = input.nextInt(); d2 = input.nextInt(); d3 = input.nextInt(); d4 = input.nextInt(); // Regions if (a1 + a2 + b1 + b2 == 10) { System.out.println ( "\nREG-1:GOOD" ); } else { System.out.println ( "\nREG-1:BAD" ); } if (c1 + c2 + d1 + d2 == 10) { System.out.println ( "REG-2:GOOD" ); } else { System.out.println ( "REG-2:BAD" ); } if (a3 + a4 + b3 + b4 == 10) { System.out.println ( "REG-3:GOOD" ); } else { System.out.println ( "REG-3:BAD" ); } if (c3 + c4 + d3 + d4 == 10) { System.out.println ( "REG-4:GOOD" ); } else { System.out.println ( "REG-4:BAD" ); } // Rows if (a1 + a2 + a3 + a4 == 10) { System.out.println ( "\nROW-1:GOOD" ); } else { System.out.println ( "\nROW-1:BAD" ); } if (b1 + b2 + b3 + b4 == 10) { System.out.println ( "ROW-2:GOOD" ); } else { System.out.println ( "ROW-2:BAD" ); } if (c1 + c2 + c3 + c4 == 10) { System.out.println ( "ROW-3:GOOD" ); } else { System.out.println ( "ROW-3:BAD" ); } if (d1 + d2 + d3 + d4 == 10) { System.out.println ( "ROW-4:GOOD" ); } else { System.out.println ( "ROW-4:BAD" ); } // comlumns if (a1 + b1 + c1 + d1 == 10) { System.out.println ( "\nCOL-1:GOOD" ); } else { System.out.println ( "\nCOL-1:BAD" ); } if (a2 + b2 + c2 + d2 == 10) { System.out.println ( "COL-2:GOOD" ); } else { System.out.println ( "COL-2:BAD" ); } if (a3 + b3 + c3 + d3 == 10) { System.out.println ( "COL-3:GOOD" ); } else { System.out.println ( "COL-3:BAD" ); } if (a4 + b4 + c4 + d4 == 10) { System.out.println ( "COL-4:GOOD" ); } else { System.out.println ( "COL-4:BAD" ); } } }
Comment