In: Computer Science
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
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.
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: