Author Topic: HI-LO Game - Using functions for subtasks  (Read 6152 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
HI-LO Game - Using functions for subtasks
« on: January 22, 2006, 11:07:55 PM »
Code: [Select]

import java.util.Scanner;

class RandomNum
{
private static int MakeRandom()
{
double x = Math.random();
return (int) Math.floor(x*100)+1;
}

public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your name: ");
String name = keyboard.next();
System.out.println("Hello " + name + "\n");

int infinite = 0;
do {

System.out.println("\nI have selected a number from 1 to 100");
System.out.println("Can you guess what this number is?");
System.out.print("Enter your guess ");
int guess = keyboard.nextInt();

int mynum = MakeRandom();
int tries = 0;
boolean isCorrect = false;
do {
tries++;
if( guess<mynum ) {
System.out.println("Your guess is too low.");
} else if ( guess>mynum ) {
System.out.println("Your guess is too high.");
} else if (guess == mynum ) {
System.out.println( guess + " is correct " + name + "!");
System.out.println( "Guessing that number took " + tries + " tries.");
isCorrect = true;
break;
}
System.out.print("Enter your guess ");
guess = keyboard.nextInt();
} while (isCorrect == false);

} while (infinite == 0);
}
}


Please ask your questions in a reply post
Analyze. Design. Develop. Debug. Deploy. Maintain.