Here is my code, for some reason it goes to an infinite loop when i try and adjust the highest and lowest values for the Math.Random() method. The code works fine if i do not try to change those values based on if the guess was higher or lower than the user's inputted number.
import java.util.Scanner;
public class ComputerGuess {
public static void main(String [] args) {
Scanner reader = new Scanner(System.in);
int highestvalue= 100;
int lowestvalue=1;
int randomnum;
int user;
String line;
int temp;
int guesses=0;
while (true){
System.out.println("Enter number for computer to guess between 1 and 100.");
user= reader.nextInt();
if (user<=100&&user>=1)
break;
else
System.out.println("Out of Range");
}
while (true){
guesses++;
randomnum=(int)(highestvalue * Math.random()) + lowestvalue;
if (user==randomnum)
{
System.out.println("The Computer Guessed Correct! It took "+guesses+" guesses!"); break;
}
temp=randomnum;
if (randomnum<user)
{highestvalue=temp;}
if (randomnum>user)
{lowestvalue=temp;}
}
}
}