-7

数独プログラムの構築に取り組んでいます。コンパイルすると、多くのエラーが発生しますが、実際のエラーはわかりません。

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

それは3つの方法で壊れています:

  • それはすべて*mainメソッドの外にあります。なぜなら、その前に余分な閉じ中括弧があるからです。
  • ifステートメントの最後にセミコロンがあります。これは{}、この場合と同じです。
  • 2セットのステートメントは、コードの他の部分のように「ブレース」されていません

さらに、明示的にと比較する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

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

true最後に、と比較する必要はありませんfalse。:if (b)このテストには問題ありません。if (b == true)引き続き機能しますが、冗長です。

于 2012-10-17T22:07:45.763 に答える