#Not running correctly

20 messages · Page 1 of 1 (latest)

naive mango
#

the % calc always comes 0 idk why

public class Main {

    public static void main(String[] args){

        int tails = 0;
        int heads = 0;
        int counter = 0;
        int runTime = 50;

        while(counter < runTime){

            counter = counter + 1;

            double result = Math.random();

            if(result < 0.5){
                tails = tails + 1;
            }else{
                heads = heads + 1;
            }


        }
        System.out.println(tails);
        System.out.println(heads);

        System.out.println(runTime);

        int headsFinal = heads / runTime;
        int tailsFinal = tails / runTime;

        System.out.println("the % of heads is " + headsFinal);
        System.out.println("the % of tails is " + tailsFinal);
        
    }
}

lime cairnBOT
#

This post has been reserved for your question.

Hey @naive mango! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tough sail
#

both headsFinal and tailsFinal should be double variables

naive mango
tough sail
#

oh and you have to cast heads to double

naive mango
#

still brings out 0.0 and 0.0

tough sail
#

otherwise its an integer division

naive mango
#

wdym

#

could u plz re write those 2 lines for me

#

im kinda confused

tough sail
#

((double) heads) /runTime

naive mango
#

Oh alr thx

#

so heads is a integer

#

but we have to make it a double

#

to divide it

#

same with tails?

tough sail
#

yeah same thing with tails

naive mango
#

its working now thx

#

!close