0

サイコロを振るプログラムのコーディングに取り組んでいます。私は学校教育のためにクラスを取っているので、まだJavaに慣れていません。私はこのプログラムの異なるパッケージで複数のクラスを使用しています。私が理解しようとしているのは、1 つのクラスで、私のパッケージ pairOfDice で、クラス pairOfDice、die1、および die2 にオブジェクトを作成したことです。これで、別のパッケージ rollDice ができました。私の目標は、pairOfDice クラスを使用して 2 つのサイコロを転がし、ロールを表示することです。私が苦労しているのは、それを正確に行う方法です。サイコロを振っているとき、サイコロを 1 つだけ振っているように結果が表示されます。ロールごとに 2 つのサイコロを表示するように調整しましたが、より熟練した方法ではないように感じます。

package die;

import java.util.Scanner;

/**
 *
 * @author <a href= "mailto:adavi125@my.chemeketa.edu" >Aaron Davis</a>
 */
public class RollDice
{
    public static void main(String[] args)
    {


        Scanner scan = new Scanner(System.in);

        PairOfDice dice = new PairOfDice();

        // get amount of rolls from user
        System.out.print("How many rolls do you want? ");

        int numRolls = scan.nextInt();



        int diceOne, diceTwo;
        int boxCar, snakeEyes;
        int j = 0, k = 0;

        // rolls the dice the requested amount of times
        for (int i = 0; i < numRolls; i++)
        {
            // first die to roll
            diceOne = dice.roll();

            // second die to roll
            diceTwo = dice.roll();

            // display rolled dice
            System.out.println(diceOne + " " + diceTwo + "\n");

            // store and display pairs of 1 rolls
            if (diceOne == 1 && diceTwo == 1)
            {
                snakeEyes = ++j;

                System.out.println("\nThe number of snake eyes you have is: " 
                    + snakeEyes + "\n");
            }


            // store and display pairs of 6 rolls
            if (diceOne == 6 && diceTwo == 6)
            {
                boxCar = ++k;

                System.out.println("\nThe number of box cars you have is: " 
                    + boxCar + "\n");
            }



        }




    }    
}


******************************************************************************
/*
 the integers diceOne and diceTwo are my workarounds, my other package contains

public class PairOfDice extends Die
{
    Die die1, die2;

    public PairOfDice()
    {
        die1 = new Die();
        die2 = new Die();     
    }

    public PairOfDice(int face)
    {
        die1 = new Die(face);
        die2 = new Die(face);
    }
}

*/
******************************************************************************

// i am un-clear how to make "PairOfDice dice = new PairOfDice();" come out as two die
4

1 に答える 1