Java错误:“else”不带“if”
我试图编译和执行一个程序,但它给了我错误: java:20:错误:“else”没有“if” 如果roll1==roll2,则为else{ ^ java:44:错误:“else”没有“if” 如果roll1==roll2,则为else{ ^ 2个错误 请帮助检查我的代码,并让我知道我可以做什么来解决这个问题。谢谢Java错误:“else”不带“if”,java,syntax,Java,Syntax,我试图编译和执行一个程序,但它给了我错误: java:20:错误:“else”没有“if” 如果roll1==roll2,则为else{ ^ java:44:错误:“else”没有“if” 如果roll1==roll2,则为else{ ^ 2个错误 请帮助检查我的代码,并让我知道我可以做什么来解决这个问题。谢谢 import java.util.Random; public class Dice { public static void main(string[] args); {
import java.util.Random;
public class Dice {
public static void main(string[] args); {
int turn;
turn = (int) (Math.random() * 2 + 1);
while (score1 <= 75 || score2 <= 75);
if (turn = 1 & (roll1 != roll2)) {
roll1 = roll1.random();
roll2 = roll2.random();
score1 += (roll1 + roll2);
System.out.prinln("Player 1 rolls a" + roll1 + "and a" + roll2);
turn = 2;
else if (roll1 == roll2) {
while (roll1 == roll2) {
System.out.println("Player 1 gets to roll again");
roll1 = roll1.random();
roll2 = roll2.random();
score1 += (roll1 + roll2);
System.out.prinln("Player 1 rolls a" + roll1 + "and a" + roll2);
score1 += (roll1 + roll2);
}
}
else {
if (score1 >= 75) {
System.out.println("Player 1 wins!");
turn = 2;
}
}
}
if (turn = 2 & roll1 != roll2) {
roll1 = roll1.random();
roll2 = roll2.random();
score2 += (roll1 + roll2);
System.out.prinln("Player 2 rolls a" + roll1 + "and a" + roll2);
turn = 1;
else if (roll1 == roll2) {
while (roll1 == roll2);
System.out.println("Player 2 gets to roll again");
score2 += (roll1 + roll2);
roll1 = roll1.random();
roll2 = roll2.random();
System.out.println("Player 2 gets to roll again");
}
else {
if (score2 >= 75) {
System.out.println("Player 2 wins!");
turn = 1;
}
}
}
}
}
首先有很多语法错误,然后是if-else条件语句错误。你需要定义一个变量,没有定义就不能使用它们 其次,您需要学习如何使用类及其方法。 在调试代码时,请查看我得到了什么,并对您需要编写的代码进行了注释 请逐步了解JAVA。 这是您可以开始的链接
搜索一些有关编写JAVA程序的视频教程。如果turn=1未比较,则在score1执行assignment时去掉此分号。}如果roll1==roll2,则在其他情况下去掉此分号{试着使用像Eclipse这样的IDE,它可能会给你更多的提示,告诉你你的代码出了什么问题。最好使用@ScaryWombat提到的IDE编写代码,还有其他错误,比如主方法中的string必须是string,score1,score2没有定义bla bla……嵌套如果else条件不正确,如果没有它,你就不能使用else请记住,有一行代码需要更改,并放入有效的java代码。我已经更改了代码,但我不太确定它是否可以工作。将下载IDE进行检查。
import java.util.Random; //Not used in your code, no need to import
public class Dice {
public static void main(String[] args) {
int turn;
turn = (int) (Math.random() * 2 + 1);
int score1 = 0;
int score2 = 0;
int roll1 = 0;
int roll2 = 0;
while (score1 <= 75 || score2 <= 75){
if (turn == 1 & (roll1 != roll2)) {
//roll1 = roll1.random(); //please correct this
//roll2 = roll2.random(); //put correct code that you want
score1 += (roll1 + roll2);
System.out.println("Player 1 rolls a" + roll1 + "and a" + roll2);
turn = 2;
}
else if (roll1 == roll2) {
while (roll1 == roll2) {
System.out.println("Player 1 gets to roll again");
//roll1 = roll1.random();
//roll2 = roll2.random();
score1 += (roll1 + roll2);
System.out.println("Player 1 rolls a" + roll1 + "and a" + roll2);
score1 += (roll1 + roll2);
}
}
else {
if (score1 >= 75) {
System.out.println("Player 1 wins!");
turn = 2;
}
}
if (turn == 2 & roll1 != roll2) {
//roll1 = roll1.random();
//roll2 = roll2.random();
score2 += (roll1 + roll2);
System.out.println("Player 2 rolls a" + roll1 + "and a" + roll2);
turn = 1;
}
else if (roll1 == roll2) {
while (roll1 == roll2);
System.out.println("Player 2 gets to roll again");
score2 += (roll1 + roll2);
//roll1 = roll1.random();
//roll2 = roll2.random();
System.out.println("Player 2 gets to roll again");
}
else {
if (score2 >= 75) {
System.out.println("Player 2 wins!");
turn = 1;
}
}
}
}
}
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}