我正在努力构建一个Sudoku程序.当我编译时,我有很多错误,但我看不出它们实际上是什么错误.
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");
Run Code Online (Sandbox Code Playgroud)
这是我的代码.
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");
}
Run Code Online (Sandbox Code Playgroud)
所有这一节都被打破了:
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");
Run Code Online (Sandbox Code Playgroud)
它以三种方式打破:
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");
}
Run Code Online (Sandbox Code Playgroud)
......但不要只是解决这个问题.重新审视您的整个设计.没有人想要阅读188行方法.您还应该考虑使用数组而不是16个单独的变量...