dimanche 5 novembre 2017

Okay so I'm coding a roll the die game on java

This is the code I have, but I want it to be able roll the die based on the number of trials the user inputs and then display the frequencies of each face. This code isn't working at I would want it to. Also I would like to change the switch cases to if and else if statements, if anybody could help me out with that would be amazing, I've been working on this for a while now.

import java.util.Random;
import java.util.Scanner;
public class DieRoll {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Random randomNumbers = new Random(); 
        int one=0;
        int two=0;
        int three=0;
        int four=0;
        int five=0;
        int six=0;
        int trials;
        int face;

        System.out.println("Please enter the number of trials");
        Scanner scan= new Scanner (System.in);
        trials= scan.nextInt();

        for(int rolls= 1; rolls==trials; rolls++);{

            face=  randomNumbers.nextInt(6) + 1;

             // determine roll value 1-6 and increment appropriate counter
             switch ( face ) 
             {   
                case 1:
                   ++one; // increment the 1s counter
                   break; 
                case 2:
                   ++two; // increment the 2s counter
                   break;
                case 3:
                   ++three; // increment the 3s counter
                   break;
                case 4:
                   ++four; // increment the 4s counter
                   break;
                case 5:
                   ++five; // increment the 5s counter
                   break;
                case 6:
                   ++six; // increment the 6s counter
                   break; // optional at end of switch
             }
          }

        System.out.println( "Face\tFrequency" ); // output headers
          System.out.printf( "1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n",
             one, two, three, four,
             five, six );

            scan.close();



    }

}

Aucun commentaire:

Enregistrer un commentaire