掷两个骰子java程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19482283/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:37:17  来源:igfitidea点击:

Roll two dice java program

java

提问by user2900919

I need to write a simple java program "Roll the Dice" with special variables: When the user rolls the dice, the application rolls two dice, displays the results of each, and asks if the user wants to roll again. Write a program that has the Main method and a separate method for the random number generator. Create a 4 integer variables to store two dice, sum of the two dice, and one for the number of times the dice are rolled. One string variable to hold the player's name and a character variable to hold a ‘y' or ‘n'. I spend an hours to try make it right but nothing really works. this what I have so far and I can't do more:

我需要编写一个带有特殊变量的简单java程序“掷骰子”:当用户掷骰子时,应用程序掷出两个骰子,显示每个骰子的结果,并询问用户是否要再次掷骰子。编写一个程序,该程序具有 Main 方法和用于随机数生成器的单独方法。创建一个 4 个整数变量来存储两个骰子,两个骰子的总和,以及一个表示掷骰子的次数。一个字符串变量用于保存玩家的姓名,一个字符变量用于保存“y”或“n”。我花了一个小时试图让它正确,但没有任何效果。这是我到目前为止所拥有的,我不能做更多:

import java.util.Random;
import java.util.Scanner;
import javax.xml.validation.Validator;

public class Main {




public static void main(String[] args) {
    System.out.println( "Welcome to the Karol's Roller Application" );
    System.out.println();

    Scanner sc = new Scanner(System.in);
    String choice = "y";

    choice = Validator.getString(sc, "Roll the Dice? (y/n): ");

    while(choice.equalsIgnoreCase("y"))
    {

        choice = Validator.getString(sc, "Roll again? (y/n): ");


        }
    }


 Random randomNumbers = new Random();{

 int dieOne = 0;
 int dieTwo = 0;
 int totals[] = new int[ 13 ];

 for( int index = 0; index < totals.length; index++ ) {
 totals[ index ] = 0;

 for( int roll = 1; roll <=4; roll++ ) {
    dieOne = (int)(Math.random()*6) + 1;
    dieTwo = (int)(Math.random()*6) + 1;
    totals[ dieOne + dieTwo ]++;}

 System.out.println( "Roll 1" +
         "\n " + dieOne + " " + 
         "\n " + dieTwo + " ");

if (totals[ dieOne + dieTwo ] == 7 )
System.out.println( "Craps!" + "\n" );

else if (totals[ dieOne + dieTwo ] == 2 )
System.out.println( "Snake eyes!" + "\n" );

else if (totals[ dieOne + dieTwo ] == 12 )
System.out.println("Box cars!" + "\n");




}

   }

}

Please, if someone can help me to do correct this program that I have some issue with, the result should look more or less like this:

拜托,如果有人能帮我纠正这个我有问题的程序,结果应该或多或少是这样的:

Welcome to the "name here" Roller Application

Roll the dice? (y/n): y

Roll 1:
number on dice one
number on dice two

Roll again? (y/n): y

Roll 2:
number on dice one
number on dice two

Roll again? (y/n): y

Roll 3:
number on dice one
number on dice two

Roll again? (y/n): y

Roll 4:
number on dice one
number on dice two

回答by Marc B

Your code is totally broken. Consider what does happen when someone does roll snakeseyes:

你的代码完全坏了。考虑一下当有人滚动蛇眼时会发生什么:

roll1 = 1
roll2 = 1
totals[roll1 + roll2]++; -> totals[2] = 1

Then later you do

然后你做

if (totals[roll1 + roll2] == 2) { ...

which will never succeed, because rolling snakeeyes will merely incremenent totals[2]to 1, not 2...

这永远不会成功,因为滚动的蛇眼只会totals[2]增加到1,而不是 2 ...

You don't need the totalsarray at all:

你根本不需要totals数组:

if(roll1 + roll2 == 7) {
  ... craps ...
} else if (roll1 + roll2 == 2) {
   ... snake eyes ...
} else etc....