Question

In: Computer Science

17. Write a program that generates a random number and asks the user to guess what...

17. Write a program that generates a random number and asks the user to guess what the

number is. If the user’s guess is higher than the random number, the program should display

“Too high, try again.” If the user’s guess is lower than the random number, the program

should display “Too low, try again.” The program should use a loop that repeats until the

user correctly guesses the random number.


18. Enhance the program that you wrote for Programming Challenge 17 so it keeps a count

of the number of guesses that the user makes. When the user correctly guesses the random

number, the program should display the number of guesses.

In Java Programming Please..

Can you please give me unique answer free from plagiarism?

I want varied opinions...

Solutions

Expert Solution

17 code:


import java.util.Scanner; 

public class Main { 

        // Function 
                public static void findNumber() 
        { 
                // Scanner Class 
                Scanner sc = new Scanner(System.in); 

                // Generate the random numbers 
                int random_number = 1 + (int)(100* Math.random()); 

                int guess; 

                // Iterate until guess number is correct
                while(true) { 

                        System.out.println(     "Guess the number:"); 

                        // input for guessing 
                        guess = sc.nextInt(); 

                        // If the number is guessed 
                        if (random_number == guess) { 
                                System.out.println("You guessed the number correctly!"); 
                                break; 
                        } 
                        else if (random_number > guess ) { 
                                System.out.println( "Too low, try again."); 
                        } 
                        else if (random_number < guess ) { 
                                System.out.println("Too high, try again."); 
                        } 
                } 
}
                
        
        public static void main(String arg[]) 
        { 

                // Function Call 
                findNumber(); 
        } 
} 

//screenshot

ouput:

18 QUESTION:

code:


import java.util.Scanner; 

public class Main { 

        // Function 
                public static void findNumber() 
        { 
                // Scanner Class 
                Scanner sc = new Scanner(System.in); 

                // Generate the random numbers 
                int random_number = 1 + (int)(100* Math.random()); 

                int guess; 
                int count=0;

                // Iterate until guess number is correct
                while(true) { 
            count++;
                        System.out.println(     "Guess the number:"); 

                        // input for guessing 
                        guess = sc.nextInt(); 

                        // If the number is guessed 
                        if (random_number == guess) { 
                                System.out.println("You guessed the number correctly!"); 
                                break; 
                        } 
                        else if (random_number > guess ) { 
                                System.out.println( "Too low, try again."); 
                        } 
                        else if (random_number < guess ) { 
                                System.out.println("Too high, try again."); 
                        } 
                        
                } 
                
                System.out.println("Number of guesses : "+count);
}
                
        
        public static void main(String arg[]) 
        { 

                // Function Call 
                findNumber(); 
        } 
} 

//screenshot

//output:

STEPS:

1.while(true) means the loop is always true and kept break in if condition when that condition is satisfied, the loop will break

2.count is 0 initially, it is used to count the number of guesses in the program


Related Solutions

In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
C++. Write a program that generates a random number between 5 and 20 and asks the...
C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses the user makes until...
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...
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
write a program that: 1) asks the user to enter grades, a negative number is the...
write a program that: 1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list) after the while loop to enter grades is finished: 2) use a for loop on the list to calculate the average of all numbers in the list 3) use a for loop on the list to find the largest number in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT