-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_guessing.java
More file actions
85 lines (72 loc) · 2.65 KB
/
Number_guessing.java
File metadata and controls
85 lines (72 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package oasis;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
public class Number_guessing implements ActionListener {
JFrame jFrameLogin;
JButton jButtonLogin;
JTextField jTextFieldUser,jTextField1;
JLabel jLabelUser;
ResultSet rs;
public Number_guessing()
{
int r = (int) (Math.random() * 100 + 1);
String s=String.valueOf(r);
jFrameLogin = new JFrame("GUESS THE NUMBER");
jFrameLogin.setResizable(false);
jFrameLogin.setBounds(440,100,400,400);
jLabelUser = new JLabel("ENTER :");
jLabelUser.setBounds(100,100,100,25);
jTextFieldUser = new JTextField("");
jTextFieldUser.setBounds(200,100,100,25);
jTextField1 = new JTextField("");
jTextField1.setBounds(200,300,100,25);
jTextField1.setText(s);
jButtonLogin = new JButton("ENTER");
jButtonLogin.setBounds(150,200,100,25);
jButtonLogin.addActionListener(this);
jFrameLogin.add(jButtonLogin);
jFrameLogin.add(jTextFieldUser);
//jFrameLogin.add(jTextField1);
jFrameLogin.add(jLabelUser);
jFrameLogin.setLayout(null);
jFrameLogin.setVisible(true);
}
public static void main(String[] args) {
Number_guessing number_guessing = new Number_guessing();
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==jButtonLogin)
{
String num = jTextFieldUser.getText();
String random = jTextField1.getText();
int n = Integer.parseInt(num);
int r = Integer.parseInt(random);
if (n==r )
{
JOptionPane.showMessageDialog(jFrameLogin,"Correct","YOU Won",JOptionPane.INFORMATION_MESSAGE);
jFrameLogin.setVisible(false);
}
else if (n<r)
{
JOptionPane.showMessageDialog(jFrameLogin,"Number is Greater","Try Again",JOptionPane.INFORMATION_MESSAGE);
}
else if (n>r)
{
JOptionPane.showMessageDialog(jFrameLogin,"Number is Smaller","Try Again",JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(jFrameLogin,"Error","Try Again",JOptionPane.INFORMATION_MESSAGE);
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}