java为什么输入else错误_Java错误:'else'没有'if'

这篇博客讨论了一段包含语法错误和条件语句问题的Java代码。作者建议使用IDE如Netbeans或Eclipse来帮助调试,并提供了对正确使用条件语句的指导,例如if-else和while循环。博客内容涵盖了随机数生成、游戏得分系统以及如何避免无限循环。
摘要由CSDN通过智能技术生成

首先,有很多语法错误,然后是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程序的视频教程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值