• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Help with understanding math.random()

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program here that requires a user to enter a guess from 1-100 and then tells the user whether he/she went to high or to low. I'm very new to JAVA and trying to figure all this stuff out..heres a copy of the program.
The program comes up with no compiler errors, but when i enter the guess the program freezes on me and doesnt go with the loop..any suggestions?

import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
public class guess
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException
{
int guess;
int randomnumber;
int number;
int count = 0;

randomnumber = (int)( Math.random() * 100 + 1);
guess = randomnumber;

System.out.print("Please enter in your guess: ");
System.out.flush();
number = Integer.parseInt(keyboard.readLine());
System.out.println();

while(number != guess);
{
count = count + 1;

if(number > guess)
System.out.print("Your number was too large, try again");
else
System.out.print("Your number was too small, try again");

System.out.print("Please enter in your guess: ");
System.out.flush();
number = Integer.parseInt(keyboard.readLine());
System.out.println();
}
System.out.println("It took you " + count + "guesses");
}


}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is the semicolon before the body of your while loop:
while(number != guess);

If you remove that semicolon (and start count at 1 rather than 0), the program seems to work fine...

[ October 14, 2004: Message edited by: marc weber ]
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you much,
Sometimes I seem to overthink the situation with programs. It's good to see that I had the whole random number thing correct, Couldnt find anything on it in the book I have. I have a handful of other programs with while and do..while loops within them that I had the semicolon there at the end, which put a hault on them aswell.
Thank you again,
Shawn
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, your use of Math.random() is correct!

There are 2 key points to this approach...

First key point: Math.random() generates a double between 0.0 and 1.0. Technically, this interval includes 0.0, but not 1.0. (I say "technically" because the chances of actually getting 0.0 is... Well, rather low.)

So multiplying the double Math.random() by 100 then gives a double between 0.0 and 100.0 -- including 0.0, but not 100.0. Adding 1 then gives a double between 1.0 and 101.0 (not including 101.0).

Second key point: Casting a floating-point primitive to a integral primitive always truncates (note that an explicit cast is always needed in these cases, even if going from a 32-bit float to a 64-bit long). So casting the above double to an int will give an int between 1 and 100 inclusively (since the double could be up to -- but not actually including -- 101.0).
[ October 15, 2004: Message edited by: marc weber ]
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic