guess my number
nessamahi (2)

Can anyone help me with this? I'm getting and string out of bounds error.
class Main {
public static void main(String[] args) {
// Implement Guess My Number here
Scanner input = new Scanner(System.in);
int n;
char response;
System.out.println("Enter n : ");
n = input.nextInt();
while(n<=0){
System.out.println("Enter a positive integer for n : ");
n = input.nextInt();
}
System.out.println("Welcome to Guess My Number! ");

	n = n-1;
	int start = 0;
	
	System.out.println("Please think of a number between " + start + " and " + n + " . ");
	
          int guess = (start+n)/2;
          while(true){
          System.out.println("Is your number : " + guess + " ?");
          
//ask user input
       System.out.println("Please enter C for correct, H for too high and L for too low ");

   response = getResponse(input);

  if (response == 'H'){
	  System.out.println("Your guess is too high");
	n = guess - 1 ;
  }
    	
 if(response == 'L') {
	System.out.println("Your guess is too low");
    start = guess + 1;
 }
}
 if (response == 'C') {
	System.out.println("you're right");
  break;

}

          guess = start+n/2;
}

}
System.out.println("Thank you for playing Guess My Number");

private static char getResponse(Scanner input){
System.out.println("Enter your response (H/L/C) : ");
String r = input.nextLine().charAt(0);

}

}

You are viewing a single comment. View All