[英]Need a little help my java code
I am teaching myself Java using Savitch's Absolute Java. 我正在使用Savitch的Absolute Java自学Java。 I am working on the programming projects: what I am asked to do is code a game of craps.
我正在从事编程项目:我被要求做的是编写掷骰子游戏。 My code works overall, but I am getting an infinite loop with a portion of it.
我的代码总体上可以正常工作,但是我得到了一部分的无限循环。 Can anybody points out what I am doing wrong?
有人可以指出我做错了吗? This is what I am asked to do: If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes “the point.” The player keeps rolling the dice until either 7 or the point is rolled.
这就是我要执行的操作: 如果将4、5、6、8、9或10滚动到出局掷骰上,则该数字变为“点”。玩家不断掷骰子直到7或该点卷起。 If the point is rolled first, then the player wins the bet.
如果该点先被掷出,则玩家赢得下注。 If a 7 is rolled first, then the player loses.
如果先掷出7,则玩家输。
Here is my code: 这是我的代码:
import java.util.Random;
public class ProgProject2
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Random randomGeneratorDice1 = new Random();
Random randomGeneratorDice2 = new Random();
//int counter = 1;
int dice1, dice2, sum, winNumber = 0, lossNumber = 0;
for (int counter = 1; counter <= 3; counter++)
{
System.out.println("Roll number " + counter + " of the dice: ");
dice1 = randomGeneratorDice1.nextInt(6) + 1;
dice2 = randomGeneratorDice2.nextInt(6) + 1;
sum = dice1 + dice2;
System.out.println("Dice1 value is: " + dice1 + " and dice2 value is: "
+ dice2);
System.out.println("The output of the dice roll is: " + sum);
if ((sum == 7) || (sum == 11))
{
System.out.println("You win!!");
winNumber++;
} // end if
else if ((sum == 2) || (sum == 3) || (sum == 12))
{
System.out.println("You lose!!");
lossNumber++;
} // end else if
**else
{
System.out.println("The point!!");
//System.out.println("The sum is: " + sum);
//int point = sum;
int sumElse;
do
{
dice1 = randomGeneratorDice1.nextInt(6) + 1;
dice2 = randomGeneratorDice2.nextInt(6) + 1;
sumElse = dice1 + dice2;
if (sumElse == sum)
{
System.out.println("You win!!");
winNumber++;
} // end inner if
else if (sumElse == 7)
{
System.out.println("You lose!!");
lossNumber++;
} // end inner else
} while ((sumElse != sum) || (sumElse != 7));
} // end else**
} // end for loop
System.out.println("Your total wins are: " + winNumber + " and your total "
+ "losses are: " + lossNumber);
double winProbability = (double) winNumber / (winNumber + lossNumber);
System.out.println("Your winning probability is: " + winProbability);
} // end main
} // end ProgProject2
Change the line 换线
} while ((sumElse != sum) || (sumElse != 7));
to 至
} while ((sumElse != sum) && (sumElse != 7));
Or you can have an infinite loop, but add break
statements (to exit the loop) in the condition bodies: 或者,您可以有一个无限循环,但可以在条件主体中添加
break
语句(退出循环):
do {
dice1 = randomGeneratorDice1.nextInt(6) + 1;
dice2 = randomGeneratorDice2.nextInt(6) + 1;
sumElse = dice1 + dice2;
if (sumElse == sum) {
System.out.println("You win!!");
winNumber++;
break;
} // end inner if
else if (sumElse == 7) {
System.out.println("You lose!!");
lossNumber++;
break;
} // end inner else
} while (true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.