数独程序无法编译

时间:2012-10-17 22:05:26

标签: java

我正在努力建立一个数独计划。当我编译时,我有很多错误,但我看不出实际的错误是什么。

   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");

这是我的代码。

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

2 个答案:

答案 0 :(得分: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非常无意义 - 而且它位于大量超长方法的末尾。但是,要使其 compile ,您可能只需要将此部分之前的结束括号移动到它之后,并将该部分更改为:

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个单独的变量...

答案 1 :(得分:4)

看起来你过早地关闭了main方法:查看编译器抱怨的第一行:if (b == true)。该行之前的右括号是关闭main方法,将其余代码留作类中的裸代码,即外部方法,这是不允许的。

同一行被分号提前终止:

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

同一个if/else语句需要一些复合语句块:

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

最后,您无需与truefalse进行比较:if (b)适用于此测试; if (b == true)仍然可以使用,但这是多余的。