Write a program that simulate a die roller. The user should type in the number of the throws.
Let the program count how many ones, twos, threes... are in the series.
In the end the program should write down how many ones ... that was and also figure out the percentage. I wrote a program but it doesn´t work. Please help.

package inlamning;
import java.text.NumberFormat; // import numberformat för att få svaret bara med en decimal
import javax.swing.*;
import java.util.*;




public class Main {



    public static void main(String[] args) {
            String s;
        s = JOptionPane.showInputDialog("Ange antal kast");
        int k = Integer.parseInt(s); // Omvandla k till int
        int summan = 0; // Ange att summan i snurran är lika med 0
        int a = 0; //Ange att a - f är lika med noll och omvandlas till int
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 0;
        
        while (summan >= k){
            Random diceRoller = new Random();
            int dice = diceRoller.nextInt(6) + 1;

            if (dice==1)
               a =a+1;
            else if (dice==2)
                b = b+1;
            else if (dice==3)
                c = c+1;
            else if (dice==4)
                d = d+1;
            else if (dice==5)
                e = e+1;
            else if (dice==6)
                f = f+1;
            summan = summan ++;
            }
        NumberFormat r = NumberFormat.getInstance();
        double ettor = a/k * 100;
        double tvaor = b/k * 100;
        double treor = c/k * 100;
        double fyror = d/k * 100;
        double femmor = e/k * 100;
        double sexor = f/k * 100;
     r.setMaximumFractionDigits(1);

     JOptionPane.showMessageDialog(null, "Du har kastat tärningen" + k + "gånger"
             + "Du har fått en etta " + a + " gånger. dvs " +  ettor + "%"
             + "Du har fått en tvåa " + b + " gånger. dvs " +  tvaor + "%"
             +"Du har fått en trea " + c + " gånger. dvs " +  treor + "%"
             +"Du har fått en fyra " + d + " gånger. dvs " +  fyror + "%"
             +"Du har fått en femma " + e + " gånger. dvs " +  femmor + "%"
             +"Du har fått en sexa " + f + " gånger. dvs " +  sexor + "%");
    }

}

Recommended Answers

All 5 Replies

while (summan >= k){

You set summan to be zero and I assume that k is something positive. Then how is it possible for 0(summan) to be greater then k ?

Also this is redundant: summan = summan ++; Try this: summan += 1; Or this: summan++;

Thank you for the answer Now it is working :)

Well, I haven't tried your code, or know what the problem is, but try this for getting your random number.

int diceRoller = (int)(Math.floor(Math.random()*(6))+1);

I didn't see the above post. Glad you got it working!

I have changed the program like this

package inlamning;

import javax.swing.*;





public class Main {



    public static void main(String[] args) {
            String s;
        s = JOptionPane.showInputDialog("Ange antal kast");
        int k = Integer.parseInt(s); // Omvandla k till int
        int summan = 0; // Ange att summan i snurran är lika med 0
        int a = 0; //Ange att a - f är lika med noll och omvandlas till int
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 0;
        
        while (summan < k){
         int dice = (int)(Math.random()*6) + 1; //random generator som talar om att det finns 6 olika sidor och den börja på +1

            if (dice==1)
               a = a+1;
            else if (dice==2)
                b = b+1;
            else if (dice==3)
                c = c+1;
            else if (dice==4)
                d = d+1;
            else if (dice==5)
                e = e+1;
            else if (dice==6)
                f = f+1;
            
           summan = summan +1;
        
        }

        int ettor = (int) a/k*100;
        int tvaor = (int) b/k*100;
        int treor =(int) c/k*100;
        int fyror = (int) d/k*100;
        int femmor =(int) e/k*100;
        int sexor =(int) f/k*100;
        
    

     JOptionPane.showMessageDialog(null, "Du har kastat tärningen" + k + "gånger"
             + "\n Du har fått en etta " + a + " gånger. dvs " +  ettor + "%"
             + "\n Du har fått en tvåa " + b + " gånger. dvs " +  tvaor + "%"
             +"\n Du har fått en trea " + c + " gånger. dvs " +  treor + "%"
             +"\n Du har fått en fyra " + d + " gånger. dvs " +  fyror + "%"
             +"\n Du har fått en femma " + e + " gånger. dvs " +  femmor + "%"
             +"\n Du har fått en sexa " + f + " gånger. dvs " +  sexor + "%");
    }

}

and now the percent doesn´t work pls help thanks

It the same problem I explained to another post. When you divide an int with an int, you get an int:

int ettor = (int) a/k*100;

a, k, 100 are ints, so the result would be int:

int/int = int
3/5 = 0 (not 0.6)
10/4 = 2 (not 2.5)

So it is better to do this:

int ettor = (int) (1.0*a/k*100);
// or
int ettor = (int) (100.0*a/k);
// or
double ettor = 100.0*a/k;

1.0*a/k*100
The first calculation would be: 1.0*a, double * int which will result to a double so then you will do double/k which will again be double

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.