Java 数独程序无法编译
我正在努力建立一个数独程序。当我编译时,我有很多错误,但我看不出它们是什么错误Java 数独程序无法编译,java,Java,我正在努力建立一个数独程序。当我编译时,我有很多错误,但我看不出它们是什么错误 if (b == true); ^ bchimedochir_Sudoku1.java:184: error: <identifier> expected if (b == true); ^ bchimedochir_Sudoku1.java:184: error: ';' expected if (b == true); ^ bch
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");
看起来您过早地关闭了main方法:查看编译器抱怨的第一行:if b==true。该行前面的右括号将关闭主方法,剩下的代码作为类中的裸代码,即在方法之外,这是不允许的 同一行被分号提前终止:
if (b == true); // <- remove the semicolon
最后,您不需要比较true和false:如果b适合此测试;如果b==true仍然有效,但它是多余的。此部分全部中断:
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个单独的变量……请在你的问题中加一点努力。当我编译时,我有很多错误,写小代码的代码,经常编译。同样,你的B==真是由半冒号终止的,所以下面的是孤立的,所有的printf语句都将被执行有效和无效一旦你应用了上面的修正你的修正代码中仍然有分号…@JonSkeet谢谢,修正了。谢谢我修正了,但它说:189:error:'else'没有'if'}else^1错误
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");
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");
}