In: Computer Science
using java
For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to choose a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or no answer. For example: Is your number evenly divisible by 3? (Yes or No).The user will enter “Yes” or “No” to each question. When the program is sure it knows the user’s number it outputs the number. For example, Your number is 9.
Your program must always guess the user’s number in less than five questions.
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! =========================================================================== import java.util.Scanner; public class GuessingGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Pick a number from 1 to 10. When ready hit a key"); scanner.nextLine(); System.out.print("Is your number a divisible by 2 (Yes or No): "); String yesOrNo = scanner.nextLine(); if (yesOrNo.equalsIgnoreCase("yes")) { System.out.print("Is your number greater than by 5 (Yes or No): "); String greater = scanner.nextLine(); if (greater.equalsIgnoreCase("yes")) { System.out.print("Is you number greater than or equal to 8 (Yes or No): "); String eight = scanner.nextLine(); if (eight.equalsIgnoreCase("Yes")) { System.out.print("Is your number divisible by 5 (Yes or No): "); String ten = scanner.nextLine(); if (ten.equalsIgnoreCase("Yes")) System.out.println("Your number is 10"); else System.out.println("Your number is 8"); } else { System.out.println("Your number is 6"); } } else { System.out.println("Is your number divisible by 4 (Yes or No): "); String four = scanner.nextLine(); if (four.equalsIgnoreCase("yes")) System.out.println("Your number is 4"); else System.out.println("Your number is 2"); } } else { System.out.print("Is your number greater than or equal to 5 (Yes or No): "); String greaterThanFive = scanner.nextLine(); if (greaterThanFive.equals("yes")) { System.out.print("Is your number divisible by 3 (Yes or No): "); String nine = scanner.nextLine(); if (nine.equalsIgnoreCase("yes")) { System.out.println("Your number is 9"); } else { System.out.print("Is your number divisible by 5 (Yes or No): "); String five = scanner.nextLine(); if (five.equalsIgnoreCase("yes")) System.out.println("Your number is 5"); else System.out.println("Your number is 7"); } } else { System.out.print("Is your number divisible by 3 (Yes or No): "); String three = scanner.nextLine(); if (three.equalsIgnoreCase("yes")) System.out.println("Your number is 3"); else System.out.println("Your number is 1"); } } } }
thank you !! : )