Question

In: Computer Science

In java Modify your finished guessNumber.java program to include a random number generator to generate the...

In java

Modify your finished guessNumber.java program to include a random number generator to generate the guessed number (from 1 to 10). use the Random class to generate a random number between a range of integers. In your program include an if … then statement to check if the number you entered is bigger or smaller than the guessed number, and display a message. Also included is a counter to keep track on how many times the user has guessed, and display it at then end.

Below is the java code that needs to be modified:

/* Week3 in-class exercise
Name: numberGuess.java
Author: Your Name
Date:

*/

// need the Scanner class to get user input
import java.util.Scanner;

public class numberGuess {

/**
* @param args
*/
public static void main(String[] args) {
// TODO:
//
// a. declare a final int, and assign a value of 6 as the guessed number
           final int guessed_number = 6;

// b. create a Scanner to get user input
           Scanner input = new Scanner(System.in);
// c. use a do {} while loop to prompt the user to enter an integer between 1 and 10,
// assign the user input to an int, and compare to the guessing number
           int userInput = 0;
do{
System.out.println("Please enter an integer between 1 and 10.");
userInput = input.nextInt();
} while (userInput != guessed_number);

// d. if the number matches the guessed number,
// print out a message that the number entered is correct.
   System.out.println("\nNice! You entered the correct number.\n");

// Here is to print your name
System.out.println("Author: Your Name");
}

}

Solutions

Expert Solution

import java.util.Random;
import java.util.Scanner;


public class GuessNumberRandom {

   public static void main(String[] args) {
      
       int cnt=1;
       // a. declare a final int, and assign a value of 6 as the guessed number
       Random randomNumbers = new Random();
final int guessed_number = randomNumbers.nextInt(10);

   //b. create a Scanner to get user input
   Scanner input = new Scanner(System.in);
   //c. use a do {} while loop to prompt the user to enter an integer between 1 and 10,
   //assign the user input to an int, and compare to the guessing number
   int userInput = 0;
   do{
       System.out.println("Please enter an integer between 1 and 10.");
       userInput = input.nextInt();
      
       if(userInput==guessed_number)
       {
           System.out.println("\nNice! You entered the correct number.\n");
           break;
       }
       else if(userInput>guessed_number)
       {
           System.out.println("Higher then number");
       }
       else if(userInput<guessed_number)
       {
           System.out.println("lower then number");
       }
       cnt+=1;
      
   } while (userInput != guessed_number);
  
   System.out.println("total attempt of correct guess is: "+cnt);
  
  
   System.out.println("Author: Your Name");
   }

}


Related Solutions

Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
A computer random number generator was used to generate 950 random digits from 0 to 9....
A computer random number generator was used to generate 950 random digits from 0 to 9. The observed frequencies of the digits are given in the table below. 0 1 2 3 4 5 6 7 8 9 88 82 97 84 87 87 95 93 90 147 Using a 0.05significance level, test the claim that all of the digits are equally likely. (a) Find the rejection region. Reject H0 if χ2> (b) Find the test statistic. (Round your final...
Question 1. Go to random.org. This website is a random number generator. Use it to generate...
Question 1. Go to random.org. This website is a random number generator. Use it to generate three numbers a, b, c between -10 and 10. Now let your a, b and c be the coefficients of the quadratic function f(x)=ax2 +bx+c. (For example, if the numbers you generated happened to be a = 2,b = 12, c = −1, your function for the rest of the question would bef(x) = 2x2 +12x−1.) (a) Put f(x) into “standard” or “vertex” formf(x)=a(x−h)2...
A computer random number generator was used to generate 550 random digits (0,1,...,9). The observed frequences...
A computer random number generator was used to generate 550 random digits (0,1,...,9). The observed frequences of the digits are given in the table below. 0 1 2 3 4 5 6 7 8 9 58 55 45 50 53 50 57 57 46 79 Test the claim that all the outcomes are equally likely using the significance level ?=0.05. The expected frequency of each outcome is E= The test statistic is ?2= The p-value is Is there sufficient evidence...
A computer random number generator was used to generate 750 random digits (0,1,...,9). The observed frequences...
A computer random number generator was used to generate 750 random digits (0,1,...,9). The observed frequences of the digits are given in the table below. 0 1 2 3 4 5 6 7 8 9 81 62 74 82 76 75 70 66 80 84 Test the claim that all the outcomes are equally likely using the significance level α=0.05α=0.05. The expected frequency of each outcome is E= The test statistic is χ2= The p-value is Is there sufficient evidence...
Using MS Excel and the random number generator function, generate values for 30 observations for the...
Using MS Excel and the random number generator function, generate values for 30 observations for the following columns with average daily: Body weight with random values between 100 and 250lbs Calories intake with random values between 1000 and 3000 calories Workout duration with random values between 0 and 60 minutes Sleep duration with random values between 2 and 12 hours Work duration with random values between 0 and 12 hours Assuming that the values are averages over 1 year, conduct...
Develop a random number generator for a Poisson distribution with mean = 10. Generate five values...
Develop a random number generator for a Poisson distribution with mean = 10. Generate five values manually with a random number table. Please show work.
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
Modify the program to double each number in the vector. #include <iostream> #include <vector> using namespace...
Modify the program to double each number in the vector. #include <iostream> #include <vector> using namespace std; int main() { const int N=8; vector<int> nums(N); // User numbers int i=0; // Loop index cout << "\nEnter " << N << " numbers...\n"; for (i = 0; i < N; ++i) { cout << i+1 << ": "; cin >> nums.at(i); } // Convert negatives to 0 for (i = 0; i < N; ++i) { if (nums.at(i) < 0) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT