首先,有很多语法错误,然后是if else条件语句错误。我在java中你需要定义一个变量,而不是定义你不能使用它们。
其次,您需要学习如何使用Class及其方法。
在调试代码时,看看我有什么,我已经对你需要编写的代码发表评论。
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;
}
}
}
}
}要摆脱语法错误,您可以使用IDE,如Netbeans,Eclipse,并了解条件语句的工作原理,请参阅
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
}请一步一步看看JAVA。
这是您可以开始Tutorials Point
的链接
搜索一些关于编写JAVA程序的视频教程。