Sally Walker
Sally Walker

Reputation: 33

Java unknown output

Soo this problem involves me rolling a pair of dice and estimate the probability that the first roll is a losing roll (2, 3, or 12).

output is the count of rolls that were losers (2,3, or 12) and calculated probability (count/N)

   public static void main(String [] args){
      int N1 = (int) (Math.random()*6 + 1); 
      int N2 = (int) (Math.random()*6 + 1); 
      int count = N1 + N2; 
           for (int i = 0; i<=1; i++)
          if (count==2 || count = 3 || count == 12)

I just don't seem to know what to do get the output...... This is my attempt

Upvotes: 0

Views: 101

Answers (2)

Stephen C
Stephen C

Reputation: 718678

The simplest way to get output from a command-line app (like you are writing) is to use System.out.println(...). For example:

int digits = 5;
System.out.println("Hello world");
System.out.println("I have " + digits + " fingers");

This should be enough of a hint for you to make progress.

Upvotes: 0

danben
danben

Reputation: 83220

It seems that you will want to roll the dice N times (where N is some large number) and count the number of times that it was a loser, correct?

So you will need to store in memory the total number of rolls, and the number of losing rolls. You can store those in int variables.

An int variable can be incremented using the ++ operator.

int rolls = 0;
rolls++;

is equivalent to

int rolls = 0;
rolls = rolls + 1;

You also don't want to call your main function a million times, so you can set the upper limit of your loop to the amount of rolls you want to have.

To calculate the probability, you will want to use floats rather than ints - you can cast an int to a float like this:

int a = 10;
float b = (float) a;

Finally, if you want to see your output via standard out, use System.out.println(). The argument to the println() function should be whatever you want to output.

Since this sounds like homework, I'm avoiding writing much code for now. Let me know if it isn't.

Upvotes: 2

Related Questions