Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Donald A. Carpenter Fred R. McFadden

1st Edition

8178088045, 978-8178088044

More Books

Students also viewed these Databases questions