掷骰子java程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26224134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dice Rolling java program
提问by user3587461
I'm making a dice rolling game! 2 dice will be rolled and 2 random numbers between 1-6 will be generated. The sum will be taken from the 2 numbers and used to decide what is next. If user's sum is 2,3,12 then they lose. If the sum is 7,11 then they win. If sum is 4,5,6,8,9,10 then the program automatically rolls the dice again until the user wins or loses. Also, after every sum displayed, underneath, display the amount of games they have won/lost. Here is my code so far:
我正在制作一个掷骰子游戏!将掷 2 个骰子,并生成 1-6 之间的 2 个随机数。总和将取自 2 个数字并用于决定下一步。如果用户的总和是 2,3,12 那么他们就输了。如果总和为 7,11,则他们获胜。如果 sum 是 4,5,6,8,9,10 则程序会自动再次掷骰子,直到用户赢或输。此外,在显示每个总和后,下方显示他们赢/输的游戏数量。到目前为止,这是我的代码:
//import java.util.Scanner;
public class Lab5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//variables
int dice1;
int dice2;
//Call the welcome method
welcome();
//fetch random numbers
/*
* **************************************************************
*welcome method
*welcome user
*no parameters
*no return
****************************************************************
*/
}
public static void welcome() {
System.out.println("Welcome to a Lucky (for me) Dice Game! \nFEELING LUCKY?!? Hope you brought lots of CASH!");{
}
int dice1=(int)(Math.random()*6+1);
int dice2=(int)(Math.random()*6+1);
int sum= dice1 + dice2;
System.out.println("Roll: total = " +sum);
if (sum==2|| sum==3|| sum==12){
System.out.println("Sorry with a " + sum + " You LOSE :("); }
else if(sum==7 || sum==11) {
System.out.println("Woah!!! With a " + sum + " You WIN!!!!!!!"); }
else{
if(sum==4 ||sum==5 ||sum==6 ||sum==8 ||sum==9 ||sum==10)
dice1=(int)(Math.random()*6+1);
dice2=(int)(Math.random()*6+1);}
int roll2 = dice1 + dice2;}
System.out.print("You rolled "+roll2+" ");{
while (roll2 !=7){
if (roll == roll2);{
System.out.println("You Win !");
break;
}else{
}
}
}}
I'm not sure how to display the games the user won/lost or how to make the program roll the dice again if they did not win/lose.
我不确定如何显示用户赢/输的游戏,或者如果他们没有赢/输,如何让程序再次掷骰子。
回答by return true
You should use a while loop: The dice are rolled again and again until the player has won or lost (then, break
ends the while loop).
您应该使用 while 循环:掷骰子一次又一次,直到玩家赢或输(然后break
结束 while 循环)。
while (true) {
int dice1=(int)(Math.random()*6+1);
int dice2=(int)(Math.random()*6+1);
int sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum==2 || sum==3 || sum==12) {
System.out.println("Sorry with a " + sum + " You LOSE :(");
break;
} else if(sum==7 || sum==11) {
System.out.println("Woah!!! With a " + sum + " You WIN!!!!!!!");
break;
}
// If you wanted, you could wait here for the user to confirm (e.g. with a key press)
}