-1

特別な変数を使用して、単純な Java プログラム "Roll the Dice" を作成する必要があります。ユーザーがサイコロを振ると、アプリケーションは 2 つのサイコロを振り、それぞれの結果を表示し、ユーザーがもう一度サイコロを振るかどうかを尋ねます。Main メソッドと、乱数発生器用の別のメソッドを持つプログラムを作成します。2 つのサイコロ、2 つのサイコロの合計、およびサイコロを振った回数を格納する 4 つの整数変数を作成します。プレーヤーの名前を保持する 1 つの文字列変数と、'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");




}

   }

}

誰かが私が問題を抱えているこのプログラムを修正するのを手伝ってくれるなら、結果は多かれ少なかれ次のようになるはずです:

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
4

1 に答える 1

0

あなたのコードは完全に壊れています。誰かがスネークアイを転がすとどうなるか考えてみましょう:

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

その後、あなたは

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

totals[2]ローリングスネークアイは 2 ではなく に増加するだけなので、これは決して成功し1ません...

totals配列はまったく必要ありません。

if(roll1 + roll2 == 7) {
  ... craps ...
} else if (roll1 + roll2 == 2) {
   ... snake eyes ...
} else etc....
于 2013-10-20T20:14:42.710 に答える