Question: The following program guesses the hidden number known by the user. Write code in place of XXX that completes the recursive function? import java.util.Scanner; public

The following program guesses the hidden number known by the user. Write code in place of XXX that completes the recursive function?
import java.util.Scanner;
public class GuessNumber {
public static void guessNumber(int lowVal, int highVal){
int midVal;
char userAnswer;
midVal =(highVal + lowVal)/2;
System.out.print("Is it "+ midVal +"?(l/h/y): ");
Scanner scanner = new Scanner(System.in);
userAnswer = scanner.next().charAt(0);
if (userAnswer !='l' && userAnswer !='h'){
System.out.println("Thank you!");
} else {
XXX
} else {
guessNumber(midVal +1, highVal);
}
}
}
public static void main(String[] args){
int lowVal =1;
int highVal =100;
guessNumber(lowVal, highVal);
}
}
Why did you need to write that code the way you did? Explain this as comments in your code.
int main(){
cout << "Choose a number from 0 to 100."<< endl;
cout << "Answer with:" << endl;
cout <<" l (your num is lower)"<< endl;
cout <<" h (your num is higher)"<< endl;
cout <<" any other key (guess is right)."<< endl;
GuessNumber(0,100);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!