Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Write a Java program which asks the user to guess a secret number by typing it into a text box.

0 votes
asked Oct 31, 2019 by anonymous
Possibility 1: The user enters the correct number. In this case, the program will congratulate the user, and
then terminate.
Possibility 2: The user enters a number which is higher than the secret number. In this case, the program
should display a dialog box which informs the user that the number is too high.
Possibility 3: The user enters a number which is too low. Again, the program should guide the user by
informing him that the number is too low.
The program should continue running until the user enters the correct number (30).

2 Answers

0 votes
answered Nov 1, 2019 by Harsha
import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

public class OnlineGDB_Doubt extends Frame implements ActionListener

{

TextField tf1;

Button b1,b2;

Label l,l2,l3;

Font f;

Random rand = new Random();  

    int rand_int = rand.nextInt(1000);  // RANDOM NUMBER BETWEEN THE 0 TO 999 if you wnat bigger number increase the 1000 value.

public void box()

{

setTitle("GUESS THE SECRET NUMBER");

setLayout(null);

tf1=new TextField();

b1=new Button("ENTER");

b2=new Button("EXIT");

l=new Label("ENTER THE SECRET NUMBER");

l3 = new Label("YOU HAVE TO GUESS THE SECRET NUMBER BETWEEN THE 0 T0 999");

f=new Font("Garmond",Font.BOLD,18);

l3.setBounds(10,45,400,30);

l.setBounds(10,120,180,30);

tf1.setBounds(200,120,170,30);

b1.setBounds(380,120,100,30);

b2.setBounds(235,190,100,30);

tf1.setFont(f);

l3.setBackground(Color.GREEN);

l.setBackground(Color.CYAN);

b1.setBackground(Color.BLUE);

b2.setBackground(Color.WHITE);

add(l);

add(tf1);

add(b1);

//add(b2);      // if you add this(removing comments) whenever you can stop otherwise it run upto ypu got the correct number.

add(l3);

setSize(600,300);

setVisible(true);

setResizable(false);

setBackground(Color.DARK_GRAY);

b1.addActionListener(this);

b2.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource() == b2)

System.exit(0);

else if(ae.getSource()==b1)

{

try

{

String s=tf1.getText();

int num = Integer.parseInt(s);

if(num == rand_int)

{

JDialog d=new JDialog(this,"ANSWER",true);

    d.setLayout(null);

l2=new Label("CONGRATULATIONS");

l2.setBounds(30,10,550,30);

tf1.setText("");

d.add(l2);

d.setSize(200,200);

d.show();

    System.exit(0);

}

else if(num > rand_int)

{

JDialog d=new JDialog(this,"ANSWER",true);

    d.setLayout(null);

l2=new Label("LARGER NUMBER");

l2.setBounds(30,10,550,30);

tf1.setText("");

d.add(l2);

d.setSize(200,200);

d.show();

}

else

{

JDialog d=new JDialog(this,"ANSWER",true);

    d.setLayout(null);

l2=new Label("SMALLER NUMBER");

l2.setBounds(30,10,550,30);

tf1.setText("");

d.add(l2);

d.setSize(200,200);

d.show();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

public static void main(String s[]) throws Exception

{

OnlineGDB_Doubt ld=new OnlineGDB_Doubt();

ld.box();

}

}

now you can enter the number in text box and guess the secret number.
0 votes
answered Nov 1, 2019 by HARSHA VARDHAN (140 points)
I AM HARSHA i glad to to reply this answer as i am good at java.if you have any doubt you can contact me through mail.

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

public class OnlineGDB_Doubt extends Frame implements ActionListener

{

TextField tf1;

Button b1,b2;

Label l,l2,l3;

Font f;

Random rand = new Random();  

    int rand_int = rand.nextInt(1000);  // RANDOM NUMBER BETWEEN THE 0 TO 999 if you wnat bigger number increase the 1000 value.

public void box()

{

setTitle("GUESS THE SECRET NUMBER");

setLayout(null);

tf1=new TextField();

b1=new Button("ENTER");

b2=new Button("EXIT");

l=new Label("ENTER THE SECRET NUMBER");

l3 = new Label("YOU HAVE TO GUESS THE SECRET NUMBER BETWEEN THE 0 T0 999");

f=new Font("Garmond",Font.BOLD,18);

l3.setBounds(10,45,400,30);

l.setBounds(10,120,180,30);

tf1.setBounds(200,120,170,30);

b1.setBounds(380,120,100,30);

b2.setBounds(235,190,100,30);

tf1.setFont(f);

l3.setBackground(Color.GREEN);

l.setBackground(Color.CYAN);

b1.setBackground(Color.BLUE);

b2.setBackground(Color.WHITE);

add(l);

add(tf1);

add(b1);

//add(b2);      // if you add this(removing comments) whenever you can stop otherwise it run upto ypu got the correct number.

add(l3);

setSize(600,300);

setVisible(true);

setResizable(false);

setBackground(Color.DARK_GRAY);

b1.addActionListener(this);

b2.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource() == b2)

System.exit(0);

else if(ae.getSource()==b1)

{

try

{

String s=tf1.getText();

int num = Integer.parseInt(s);

if(num == rand_int)

{

JDialog d=new JDialog(this,"ANSWER",true);

    d.setLayout(null);

l2=new Label("CONGRATULATIONS");

l2.setBounds(30,10,550,30);

tf1.setText("");

d.add(l2);

d.setSize(200,200);

d.show();

    System.exit(0);

}

else if(num > rand_int)

{

JDialog d=new JDialog(this,"ANSWER",true);

    d.setLayout(null);

l2=new Label("LARGER NUMBER");

l2.setBounds(30,10,550,30);

tf1.setText("");

d.add(l2);

d.setSize(200,200);

d.show();

}

else

{

JDialog d=new JDialog(this,"ANSWER",true);

    d.setLayout(null);

l2=new Label("SMALLER NUMBER");

l2.setBounds(30,10,550,30);

tf1.setText("");

d.add(l2);

d.setSize(200,200);

d.show();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

public static void main(String s[]) throws Exception

{

OnlineGDB_Doubt ld=new OnlineGDB_Doubt();

ld.box();

}

}

now you can guess the secret the number.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...