-7

I'm trying to create a java program for my class that throws 3 dice. The player rolls the dice and earns a profit in dollars based on the three values obtained. In particular, the profit is equal to the number of even values multiplied by the sum of those values, plus the number of odd values multiplied by the sum of those values.

For example, if the player rolls 4, 5, and 2 we calculate:

2 x 6 (two even values summing to 6)

And

1 x 5 (one odd value summing to 5). So the profit is 12 + 5 = $17.

Here is what I have so far -

import java.util.Scanner;
public class Maiorca_Hw2
{
public static void main (String [] args)
  {

  Scanner keyboard = new Scanner (System.in);
  System.out.println("Enter Values of Dice 1");
    d1 = keyboard.nextDouble();
  System.out.println("Enter Values of Dice 2");
    d2 = keyboard.nextDouble();
  System.out.println("Enter Values of Dice 3");
    d3 = keyboard.nextDouble();

  double numbers = 1,2,3,4,5,6;
  double profit, dice, d1, d2, d3, oddsum, evensum;

  if (d1 == 1 || d1 == 3 || d1 == 5)
    oddsum = 
Troy
  • 11
  • 2

2 Answers2

2

I personally think that the program should be done in a different way and you should also use clases.

I hope this little code helps you!

First of all we must create a class called Dice, where we will set the behaviour of a dice and the interactions allowed. You say that the player rolls, so there must be a random event, I think.

//CODE:

import java.util.Random;

public class Dice {

    private final int SIDES;

    //Constructor for a dice
    public Dice(int sides){
        //Establishes the number of sides of the dice we're using.
        SIDES = sides;
    }

    //Methods:

    public int roll(){

        Random rnd = new Random();

        return rnd.nextInt(SIDES) + 1;
    }
}

After that, in the main class, we put it to the test. The why of everything is in the comments

//CODE:

public class MainClass {

    public static void main(String[] args) {
        int moneyEarned = 0;
        //We generate 3 dices with 6 sides each.
        Dice d1 = new Dice(6);
        Dice d2 = new Dice(6);
        Dice d3 = new Dice(6);

        // Then we start the simulation:

        int res1, res2, res3;

        res1 = d1.roll();
        res2 = d2.roll();
        res3 = d3.roll();

        //Show results:
        System.out.println("Roll results: dice 1 = " + res1 +
            "; dice 2 = " + res2 + "; dice 3 = " + res3);


        //We obtain the money the player must earn:
        //An even number modulo 2 can only be either 1 or 0 so the same number + 1 will
        //switch from 0 to 1 and the opposite thing.


        //With this number we can choose which numbers we want to sum:

        int oddNumbersPoints = ((res1 * (res1%2))+(res2 * (res2 % 2))+( res3 * (res3 % 2)));

        int evenNumbersPoints = ((res1 * ((res1 + 1) %2)) + (res2 * ((res2+1) % 2))+( res3 * ((res3+1) % 2)));

        moneyEarned = (evenNumbersPoints * 2) + oddNumbersPoints; 

        //Finally display the final result:
        System.out.println("Even Numbers Points: " + evenNumbersPoints);
        System.out.println("Odd Numbers Points: " + oddNumbersPoints);
        System.out.println("Congratulations, you just earned: " + moneyEarned + "$");

    }
}

///// Finally you can change the main class to change the number of dices, the scores, so on...

(Add '}' and the main class head)

sfedak
  • 676
  • 6
  • 20
José Vega
  • 31
  • 5
  • 1
    Maybe, but what he will do with the info is his business. If he wants to learn something he will take the idea and he will do it by himself, otherwise he will learn nothing, of course. – José Vega Mar 25 '15 at 23:25
0

What you can do declaring primitive types, in this case double, is:

double profit = 0, dice = 2, d2 = 3;

To declare an array of doubles:

double[] numbers = {1,2,3,4,5,6};
WonderWorld
  • 956
  • 1
  • 8
  • 18