-7

I am working on to build a Sudoku programm. when i compile, i have many errors but i can't see the actual what error were they.

   if (b == true);
    ^
bchimedochir_Sudoku1.java:184: error: <identifier> expected
    if (b == true);
         ^
bchimedochir_Sudoku1.java:184: error: ';' expected
    if (b == true);
            ^
bchimedochir_Sudoku1.java:184: error: illegal start of type
    if (b == true);
                 ^
bchimedochir_Sudoku1.java:184: error: <identifier> expected
    if (b == true);
                  ^
bchimedochir_Sudoku1.java:184: error: ';' expected
    if (b == true);
                   ^
bchimedochir_Sudoku1.java:186: error: illegal start of type
    System.out.println("Congratulation !\n");
          ^
bchimedochir_Sudoku1.java:186: error: ';' expected
    System.out.println("Congratulation !\n");
              ^
bchimedochir_Sudoku1.java:186: error: invalid method declaration; return type required
    System.out.println("Congratulation !\n");
               ^
bchimedochir_Sudoku1.java:186: error: illegal start of type
    System.out.println("Congratulation !\n");
                       ^
bchimedochir_Sudoku1.java:187: error: <identifier> expected
    System.out.println("This Sudoku is valid.\n");
                      ^
bchimedochir_Sudoku1.java:187: error: illegal start of type
    System.out.println("This Sudoku is valid.\n");
                       ^
bchimedochir_Sudoku1.java:188: error: illegal start of type
    else
    ^
bchimedochir_Sudoku1.java:188: error: ';' expected
    else
        ^
bchimedochir_Sudoku1.java:190: error: illegal start of type
        System.out.println("Sorry !\n");
              ^
bchimedochir_Sudoku1.java:190: error: ';' expected
        System.out.println("Sorry !\n");
                  ^
bchimedochir_Sudoku1.java:190: error: invalid method declaration; return type required
        System.out.println("Sorry !\n");
                   ^
bchimedochir_Sudoku1.java:190: error: illegal start of type
        System.out.println("Sorry !\n");
                           ^
bchimedochir_Sudoku1.java:191: error: <identifier> expected
    System.out.println("This Sudoku is invalid.\n");
                      ^
bchimedochir_Sudoku1.java:191: error: illegal start of type
    System.out.println("This Sudoku is invalid.\n");
                       ^
20 errors
bchimedochir@loki:~/F12_1400-005_prog7$ vim bchimedochir_Sudoku1.java
bchimedochir@loki:~/F12_1400-005_prog7$ javac bchimedochir_Sudoku1.java
bchimedochir_Sudoku1.java:184: error: illegal start of type
    if (b == true);
    ^
bchimedochir_Sudoku1.java:184: error: <identifier> expected
    if (b == true);
         ^
bchimedochir_Sudoku1.java:184: error: ';' expected
    if (b == true);
            ^
bchimedochir_Sudoku1.java:184: error: illegal start of type
    if (b == true);
                 ^
bchimedochir_Sudoku1.java:184: error: <identifier> expected
    if (b == true);
                  ^
bchimedochir_Sudoku1.java:184: error: ';' expected
    if (b == true);
                   ^
bchimedochir_Sudoku1.java:185: error: illegal start of type
    System.out.print("Congratulation !\n");
          ^
bchimedochir_Sudoku1.java:185: error: ';' expected
    System.out.print("Congratulation !\n");
              ^
bchimedochir_Sudoku1.java:185: error: invalid method declaration; return type required
    System.out.print("Congratulation !\n");
               ^
bchimedochir_Sudoku1.java:185: error: illegal start of type
    System.out.print("Congratulation !\n");
                     ^
bchimedochir_Sudoku1.java:186: error: <identifier> expected
    System.out.print("This Sudoku is valid.\n");
                    ^
bchimedochir_Sudoku1.java:186: error: illegal start of type
    System.out.print("This Sudoku is valid.\n");
                     ^
bchimedochir_Sudoku1.java:187: error: illegal start of type
    else
    ^
bchimedochir_Sudoku1.java:187: error: ';' expected
    else
        ^
bchimedochir_Sudoku1.java:188: error: illegal start of type
        System.out.print("Sorry !\n");
              ^
bchimedochir_Sudoku1.java:188: error: ';' expected
        System.out.print("Sorry !\n");
                  ^
bchimedochir_Sudoku1.java:188: error: invalid method declaration; return type required
        System.out.print("Sorry !\n");
                   ^
bchimedochir_Sudoku1.java:188: error: illegal start of type
        System.out.print("Sorry !\n");
                         ^
bchimedochir_Sudoku1.java:189: error: <identifier> expected
    System.out.print("This Sudoku is invalid.\n");
                    ^
bchimedochir_Sudoku1.java:189: error: illegal start of type
    System.out.print("This Sudoku is invalid.\n");

Here is my code.

import java.util.Scanner;

public class bchimedochir_Sudoku 
{
public static void main(String args[])

    {   
Scanner input = new Scanner(System.in);

    int r1c1;
    int r1c2;
    int r1c3;
    int r1c4;
    int r2c1;
    int r2c2;
    int r2c3;
    int r2c4;
    int r3c1;
    int r3c2;
    int r3c3;
    int r3c4;
    int r4c1;
    int r4c2;
    int r4c3;
    int r4c4;
    int total;
    boolean b = true;


        System.out.printf("Welcome to Sudoku Checker v1.0!\n");
        System.out.printf("This program checks simple, small, 4X4 Sudoku grids for\n");
        System.out.printf("correctness. Each column, row and 2X2 region contains the numbers\n");
        System.out.printf("1 through 4 only once.\n\n");
        System.out.printf("To check your Sudoku, enter your board one row at a time, with\n");
        System.out.printf("Each digit separated by a space. Hit enter at the end of the row.\n\n");

        System.out.print("Enter Row 1 :");
        r1c1 = input.nextInt();
        r1c2 = input.nextInt();
        r1c3 = input.nextInt();
        r1c4 = input.nextInt();

        System.out.print("Enter Row 2 :");
        r2c1 = input.nextInt();
        r2c2 = input.nextInt();
        r2c3 = input.nextInt();
        r2c4 = input.nextInt();

        System.out.print("Enter Row 3 :");
        r3c1 = input.nextInt();
        r3c2 = input.nextInt();
        r3c3 = input.nextInt();
        r3c4 = input.nextInt();

        System.out.print("Enter Row 4 :");
        r4c1 = input.nextInt();
        r4c2 = input.nextInt();
        r4c3 = input.nextInt();
        r4c4 = input.nextInt();

        System.out.print("Thank you. Now Checking ...\n\n");

        sum = r1c1 + r1c2 + r1c3 + r1c4;
        if (total == sum) 
        {
          System.out.print("ROW-1:PASS\n");
        } else 
        {
            b = false;
            System.out.print("ROW-1:FAILED\n");
        }

        sum = r2c1 + r2c2 + r2c3 + r2c4;
        if (total == sum) 
        {
            System.out.print("ROW-2:PASS\n");
        } else 
        {
            b = false;
            System.out.print("ROW-2:FAILED\n");
        }

        sum = r3c1 + r3c2 + r3c3 + r3c4;
        if (total == sum) 
        {
            System.out.print("ROW-3:PASS\n");
        } else 
        {
            b = false;
            System.out.print("ROW-3:FAILED\n");
        }

        sum = r4c1 + r4c2 + r4c3 + r4c4;
        if (total == sum) 
        {
            System.out.print("ROW-4:PASS\n");
        } else 
        {
            b = false;
            System.out.print("ROW-4:FAILED\n");
        }

        sum = r1c1 + r2c1 + r3c1 + r4c1;
        if (total == sum) 
        {
            System.out.print("COL-1:PASS\n");
        } else 
        {
            b = false;
            System.out.print("COL-1:FAILED\n");
        }

        sum = r1c2 + r2c2 + r3c2 + r4c2;
        if (total == sum) 
        {
            System.out.print("COL-2:PASS\n");
        } else 
        {
            b = false;
            System.out.print("COL-2:FAILED\n");
        }

        sum = r1c3 + r2c3 + r3c3 + r4c3;
        if (total == sum) 
        {
            System.out.print("COL-3:PASS\n");
        } else 
        {
            b = false;
            System.out.print("COL-3:FAILED\n");
        }

        sum = r1c4 + r2c4 + r3c4 + r4c4;
        if (total == sum) 
        {
            System.out.print("COL-4:PASS\n");
        } else 
        {
            b = false;
            System.out.print("COL-4:FAILED\n");
        }

        sum = r1c1 + r1c2 + r2c1 + r2c2;
        if (total == sum) 
        {
            System.out.print("REG-1:PASS\n");
        } else 
        {
            b = false;
            System.out.print("REG-1:FAILED\n");
        }

        sum = r3c1 + r3c2 + r4c1 + r4c2;
        if (total == sum) 
        {
            System.out.print("REG-2:PASS\n");
        } else 
        {
            b = false;
            System.out.print("REG-2:FAILED\n");
        }

        sum = r1c3 + r1c4 + r2c3 + r2c4;
        if (total == sum) 
        {
            System.out.print("REG-3:PASS\n");
        } else 
        {
            b = false;
            System.out.printf("REG-3:PASS %d\n",sum);
            System.out.print("REG-3:FAILED\n");
        }

        sum = r3c3 + r3c4 + r4c3 + r4c4;
        if (total == sum) 
        {
            System.out.print("REG-4:PASS\n");
        } else 
        {
            b = false;
            System.out.print("REG-4:FAILED\n");
        }
    }
       if (b == true); 

            System.out.println("Congratulation !\n");
            System.out.println("This Sudoku is valid.\n");
         else 

            System.out.println("Sorry !\n");
            System.out.println("This Sudoku is invalid.\n");
}
4

2 回答 2

7

此部分的所有内容都已损坏:

if (b == true); 

    System.out.println("Congratulation !\n");
    System.out.println("This Sudoku is valid.\n");
 else 

    System.out.println("Sorry !\n");
    System.out.println("This Sudoku is invalid.\n");

它分为三个方面:

  • 这一切都在main方法之外,因为你在它之前有一个额外的右大括号
  • 它在语句的末尾有一个分号,在这种情况下if相当于{}
  • 您的两组语句不像其他代码那样“支撑”

此外,与true显式比较是丑陋的,变量名也毫无意义——而且它位于一个超长方法b的末尾。但是,要使其编译,您可能只需将本节之前的右大括号移动到它之后,并将该部分更改为:

if (b)
{
    System.out.println("Congratulation !\n");
    System.out.println("This Sudoku is valid.\n");
}
else 
{
    System.out.println("Sorry !\n");
    System.out.println("This Sudoku is invalid.\n");
}

...但不要只是解决这个问题。重新审视你的整个设计。没有人愿意阅读 188 行的方法。您还应该考虑使用数组而不是使用 16 个单独的变量...

于 2012-10-17T22:10:09.613 回答
4

It looks like you're closing your main method prematurely: look at the first line that the compiler complains about: if (b == true). The closing brace before that line is closing your main method, leaving the rest of the code as bare code in the class, i.e. outside a method, which isn't allowed.

The same line is prematurely terminated by a semicolon:

if (b == true); // <- remove the semicolon

And the same if/else statement needs some compound statement blocks:

if (b == true)
{  // add the block
   System.out.println("Congratulation !\n");
   System.out.println("This Sudoku is valid.\n");
}
else 
{  // ditto here
   System.out.println("Sorry !\n");
   System.out.println("This Sudoku is invalid.\n");
}

Finally, you don't need to compare against true and false: if (b) is fine for this test; if (b == true) will still work, but it's redundant.

于 2012-10-17T22:07:45.763 回答