jeudi 22 octobre 2015

How do i convert an if-else statement to a switch statement

I have this program of rolling dice by 1000000 and finding the number of 1's, 2's, 3's, 4's, 5's, 6's`but i need to make a switch statement as well. Im having trouble using the scanner input

import java.util.Random;

public class DiceRoll_NP {

    public static void main(String[] args) {
        Random rand = new Random();
        final double NUMBER_OF_ROLLS = 1000000.0;

        int x = rand.nextInt();
        // System.out.println(x);

        int ones = 0, twos = 0, threes = 0, fours = 0, fives = 0, sixes = 0;

        for (int i = 1; i <= NUMBER_OF_ROLLS; i = i + 1) {
            int y = rand.nextInt(6) + 1;
            if (y == 1) ones++;
            else if (y == 2) twos++;
            else if (y == 3) threes++;
            else if (y == 4) fours++;
            else if (y == 5) fives++;
            else if (y == 6) sixes++;
            System.out.print(y + " ");
        }
        System.out.printf("\nOnes: %.2f%%\n", 100 * ones / NUMBER_OF_ROLLS);
        System.out.printf("Twos: %.2f%%\n", 100 * twos / NUMBER_OF_ROLLS);
        System.out.printf("Threes: %.2f%%\n", 100 * threes / NUMBER_OF_ROLLS);
        System.out.printf("Fours: %.2f%%\n", 100 * fours / NUMBER_OF_ROLLS);
        System.out.printf("Fives: %.2f%%\n", 100 * fives / NUMBER_OF_ROLLS);
        System.out.printf("sixes: %.2f%%\n", 100 * sixes / NUMBER_OF_ROLLS);
    }

}

Aucun commentaire:

Enregistrer un commentaire