dimanche 25 décembre 2016

Why isn't this Math.random method working?

Since Java doesn't allow to return two types in one method, I thought best way to do it is to use get methods.

Simply, I wanted computer to generate two random numbers, and if they were not the same I wanted it to print sum of them. If they were the same, I wanted it to roll once more and sum all of the rolls. Until here, it was okay, but then I wanted to see not only sum, but also the numbers that computer generated randomly before adding them up. Therefore, it had to be several return types.

Can you help me with this? I want to learn what is wrong exactly with this code and if it can be done neater and cleaner? I know Java loves long ways..

Thank you.

class App {

    public static int monopolyRoll(int side) {

        double randomNumber = Math.random();

        randomNumber = randomNumber * side;

        randomNumber = randomNumber + 1;

        int randomInt = (int) randomNumber;

        return randomInt;

    }

    private int roll1 = monopolyRoll(6);
    private int roll2 = monopolyRoll(6);

    public int userRolls() {

        if (roll1 != roll2) {

            return roll1 + roll2;

        } else {

            int roll3 = monopolyRoll(6);
            int roll4 = monopolyRoll(6);

            return roll1 + roll2 + roll3 + roll4;
        }
    }

    private static int first;
    private static int second;
    private static int third;

    public App(int first, int second, int third) {
        App.first = roll1;
        App.second = roll2;
        App.third = userRolls();
    }

    public static int getFirst() {
        return first;
    }

    public static int getSecond() {
        return second;
    }

    public static int getThird() {
        return third;
    }

    public static void main(String[] args) {

        int first = getFirst();
        int second = getSecond();
        int third = getThird();

        System.out.println(first);
        System.out.println(second);
        System.out.println(third);

    }

}




Aucun commentaire:

Enregistrer un commentaire