Question

In: Computer Science

using java For this assignment, you will write a program that guesses a number chosen by...

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.

Solutions

Expert Solution

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 !! : )


Related Solutions

For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Using files, Write a Java program to read 2 tutorials group of student id, assignment mark...
Using files, Write a Java program to read 2 tutorials group of student id, assignment mark (30), lab test mark(20) from 2 input files namely group1.txt and group 2.txt. Your program will calculate the total course work marks and write in to 3 output files with the following conditions: if the total course work marks are more than 40 out 50 write in to LIST1.TXT if the total course work marks are between 30 to 40 then write in to...
Language is Java Design and write a Java console program to estimate the number of syllables...
Language is Java Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one. The program should prompt for a word and respond with the estimated number of syllables in...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT