Re: Think of a number Program
Code:
import java.util.*;
public class ThinkNumber
{
public static void main(String [] args)
{
Scanner myKeyboard = new Scanner(System.in);
{
int randomNumber = (int)(Math.random() * 100) + 1;
System.out.print("Guess a number between 1 and 100: ");
{
int guess = myKeyboard.nextInt();
int attempt = 1;
while(guess != randomNumber)
{
if(guess < randomNumber)
{
System.out.println("Too Low");
System.out.println("Please Guess Again");
}
else if(guess > randomNumber)
{
System.out.println("Too High");
System.out.println("Please Guess Again");
}
guess = myKeyboard.nextInt();
attempt++;
}
{
System.out.println("Correct! You win!");
System.out.print("Your Number Of Attempts Was " + attempt);
}
}
}
}
}
thank you all very much for your help on this
many thanks Andy.
Re: Think of a number Program
I'm glad you've got it sorted out.
Don't go overboard with the braces. Basically, use a pair of braces with
*if
*else
*while
*for
and one pair with each
*class
*method
But not otherwise (or you may cause things to break). Also it's a good idea to indent the lines that go between braces. nested braces: more indent.
[edit] I realise you probably don't want to muck around with code that does what it's supposed to! But you can always revert back if anything goes wrong, or ask here.