I am trying to create a program in Java in which the computer randomly guesses a number between 1-100 and allows the user to guess to the number.
If the number is lower
You forgot to get a new int from the scanner in each loop :)
import java.util.Scanner;
public class QuestionOne
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
int a = 1 + (int) (Math.random() * 99),
guess,
count = 0;
System.out.println("I am thinking of a number from 1 to 100 ... guess what it is ?");
while((guess = keyboard.nextInt()) != a){
if (guess > a)
{
System.out.println("lower!");
}
else
{
System.out.println("higher!");
}
count++;
}
System.out.println("Congratulations. You guessed the number with "+ count +" tries!");
}
}
edit : I'm currently bored... Add the counter ;)