Question

In: Computer Science

We are going to develop a guessing game where the user has to guess a secret...

We are going to develop a guessing game where the user has to guess a secret number.

We will start from a simple task and refine our code step by step.

Task is to develop a program that

  1. Generates a random integer number in the range of [1-10] as the secret number
  2. Asks for user to input an integer in the range of [1-10]
  3. Print out the secret number and a user's input.

Steps

Please download this sample code, using Netbeans revise it to satisfy the above requirements. Please submit a text entry of your successful code to this assignment.

/* * To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

// We need to import the following library to display the message dialog window.

// import javax.swing.JOptionPane;/

* This is a list of functions we need to use.

* 1. Create an integer variable:* int integerName = 0;

* 2. Collect user input from a dialog:

* String userInput = JOptionPane.showInputDialog("your message");

* 3. Print out a message using a message dialog:

* JOptionPane.showMessageDialog(null, "your message", "title of the dialog", JOptionPane.PLAIN_MESSAGE);*

* 4. When we use the dialog message, at the very end, we need to add one line of code:

* System.exit(0);* 5. Convert a string to an integer

* int intNum = Integer.parseInt(stringVariable);

* 6. Generate a random integer in the range of [minimum, maximum]* int random = minimum + (int) (Math.random() * maximum);

* 7. Decision Structure* if(condition) {* your instructions ...* } else {* your instructions ...* }*

* 8. Loop Structure* while (condition) {* your instructions ...* }*

*/package numberguessgame;import javax.swing.JOptionPane;public class NumberGuessGame { /** * @param args the command line arguments */ public static void main(String[] args) { int integername = 0; int minimum = 0; int maximum = 10; String userInput = JOptionPane.showInputDialog("Guess a number between 0 and 10"); /*System.out.println(userInput);*/ int intNum = Integer.parseInt(userInput); JOptionPane.showMessageDialog(null, intNum, "Your guess",

JOptionPane.PLAIN_MESSAGE); int random = minimum + (int) (Math.random() * maximum); /*System.out.println(random);*/ JOptionPane.showMessageDialog (null, random, "The correct number", JOptionPane.PLAIN_MESSAGE); }}

HINT: You need functions one through to six from the sample code above.

Solutions

Expert Solution

Please find below code and don't forget to give a Like.

Please refer below screenshot and output.

Code:

import java.lang.Math;
import java.util.Scanner;
public class Main
{   
   public static void main(String[] args) {
   int min=1;
int max=10;
       int secret_number=(int)(Math.random()*(max-min+1)+min);
       Scanner input=new Scanner(System.in);
       System.out.print("Enter number: ");
       int user_number=input.nextInt();
       System.out.println("---------------------------");
       System.out.println("The Secret number: "+secret_number+"\nUser number: "+user_number);
   }
}

Output:


Related Solutions

guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1 and 100. Console Welcome to the Guess the Number Game ++++++++++++++++++++++++++++++++++++ I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 You got it in 1 tries. Great work! You are a mathematical wizard. Try again? (y/n): y I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 Way too high! Guess...
Write a game where a user has to guess a number from 1 – 6, inclusive....
Write a game where a user has to guess a number from 1 – 6, inclusive. Your program will generate a random number once (pick a number), and will then prompts the user to guess the number for up to 3 times. If the user enters 3 wrong guesses, the program should be terminated along with the losing message. Once the user has successfully guessed the number, tell the user they win, and tell them how many guesses it took...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
Guessing game - when I guess, the guess that I make doesn't check correct even though...
Guessing game - when I guess, the guess that I make doesn't check correct even though it is correct. // use external file: line1 linetwo line three linefour line five // end external file. // begin program: #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <time.h> #include <stdlib.h> using namespace std; string trimWSFuntion(string); void guessMess(string, string); int main() { //to store 3 lines string lineFromWordtxt1; string lineFromWordtxt2; string lineToTrim; ifstream myFile; string read_file_name; // this block gets the filename...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Create a Guess the Number game. Python randomly chooses a secret four-digit number (with no repeating...
Create a Guess the Number game. Python randomly chooses a secret four-digit number (with no repeating digits) and asks the user to guess it. The user has up to ten attempts to guess the number. After each guess, Python gives the user two clues (until the user either guesses the number correctly or runs out of attempts): The number of digit(s) in the user’s guess that is (are) both correct and in the right position. The number of digit(s) in...
C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
Develop a Java application that plays a "guess the number" game as described below. a) The...
Develop a Java application that plays a "guess the number" game as described below. a) The user interface is displayed and the user clicks the “Start Game” button to begin the game. b) Your application then gets a random number in the range 1-1000 inclusive (you might want to use Math.random or the Random class). c) The application then displays the following prompt (probably via a JLabel): I have a number between 1 and 1000 can you guess my number?...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT