-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_11.java
More file actions
68 lines (56 loc) · 2.58 KB
/
task_11.java
File metadata and controls
68 lines (56 loc) · 2.58 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
import javax.swing.*;
import java.awt.*;
import java.net.JarURLConnection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class task_11 {
public static class App extends JFrame {
JFrame jf = new JFrame("TEST");
Image icon = Toolkit.getDefaultToolkit().getImage("src/fon_1.jpg");
//Image fon = new ImageIcon("src/fon_2.png").getImage();
JTextField sample = new JTextField();
int attempt = 0;
int rndm = 1+(int)(Math.random()*20);
boolean win = false;
App() {
jf.setSize(400, 300);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
jf.setIconImage(icon);
jf.setLayout(new GridLayout(2, 3));
sample.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Дебаг ответа " + rndm);
System.out.println("Попытка номер: " + ++attempt);
if(Integer.parseInt(sample.getText()) < rndm){
JOptionPane.showMessageDialog(App.this,
"Ваше число меньше заданного"); }
if(Integer.parseInt(sample.getText()) > rndm){
JOptionPane.showMessageDialog(App.this,
"Ваше число больше заданного"); }
if(Integer.parseInt(sample.getText()) == rndm){
win = true;
JOptionPane.showMessageDialog(App.this,
"УРА! ЭТО ОНО!"); }
if (win || attempt == 3) {
attempt = 0;
rndm = 1 + (int) (Math.random() * 20);
if (win)
JOptionPane.showMessageDialog(App.this,
"Победа! Игра начинается заново");
else
JOptionPane.showMessageDialog(App.this,
"Потрачено три попытки! Игра начинается заново");
win = false;
}
}
});
jf.add(sample);
jf.setVisible(true);
JOptionPane.showMessageDialog(null,
"Игра началась, введите число от 1 до 21");
}
}
public static void main(String[] args) {
new App();
}
}