heres the code:
Scanner sc = new Scanner(System.in); Random rand = new Random(); boolean running = true; String yesno; String y = "y"; String n = "n"; boolean guessrunning = true; int i = 0; int low = 0; int high = 0; boolean highlow = true; int guess = 0; boolean guesscheck = true; System.out.println("Would you like play? Y/N"); while (running == true) { yesno = sc.next(); if (yesno.compareToIgnoreCase(y) == 0) { while (highlow == true) { System.out.println("please enter the highest number"); high = sc.nextInt(); System.out.println("please enter the lowest number"); low = sc.nextInt(); if (high - low <= 0) { System.out.println("Please enter a high number which is higher than the low number"); } else { highlow = false; } } highlow = true; System.out.println("Please enter your Guesses"); int randomnum = rand.nextInt(high - low) + low; System.out.println((int) Math.sqrt(high - low)); for (i = 0; i < (int) Math.sqrt(high - low); i++) { while (guesscheck = true) { guess = sc.nextInt(); if (guess > high || guess < low) { System.out.println("Please enter a number that is between your low and high numbers"); } else { guesscheck = false; if (guess == randomnum) { System.out.println("congratulations!! you got it right!"); i = (int) Math.sqrt(high - low); } else { System.out.println("Wrong, try again"); } guesscheck = false; } } } System.out.println("Play again? Y/N"); } else if (yesno.compareToIgnoreCase(n) == 0) { running = false; } else { System.out.println("Please enter Y for yes or N for no"); } }
it does have stuff before and after it, thats the core of it though
the problem is after you have entered high and low, you then have infinite guessses, despite my best attempts at making you only have the square root of high - low guesses
this is a problem that i am doing for school, thats why its crazy, but its no assessment task, so no holding back
thanks in advance