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...
In this programming assignment, you are asked to write a multi-threaded program using JAVA to compute...
In this programming assignment, you are asked to write a multi-threaded program using JAVA to compute the sum of an arithmetic series from 1 to 200 using pthreads.   The main thread is responsible for creating two child threads The first child thread should compute the partial sum of the series from 1 to 100 and the second computes the partial sum of another series from 101 to 200. The main thread should accumulate the partial sums computed by the child...
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...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
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...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer to do the following: 1) Create a bank account by supplying a user id and password .2) Login using their id and password 3) Quit the program. Now if login was successful the user will be able to do the following: 1) Withdraw money. 2) Deposit money. 3) Request balance. 4) Quit the program. If login was not successful (for example the id or...
Week 4 Assignment: What’s in a Number? Directions: You are to write a C++ program that...
Week 4 Assignment: What’s in a Number? Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: Your C++ source code file. (The file with the .CPP extension).No other files will be accepted. A screenshot of your program running. Program Instructions: To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to...
You are to write a program using Java that will simulate a slot machine with four...
You are to write a program using Java that will simulate a slot machine with four wheels. It will determine the amount of money won for each spin. The four wheels spin and stop showing a value between 0 and 9 inclusive. It costs $2 to play. •You win $500 (but not the $2 you bet) if you get 4 wheels the same. •You win $10 (but not the $2 you bet) if you get exactly 3 of a kind....
Write a program in java that asks the name of the buyer and the number of...
Write a program in java that asks the name of the buyer and the number of shares bought. Your program must then calculate and display the following. sold stock/shares to the general public at the rate of $24.89 per share. Theres a 2 percent (2%) commission for the transaction. Outputs need Amount paid for buying the stock ($) Amount paid for the commission ($) Total amount ($)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT