Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. if(guess == number){
  2. l1.setText("You have won the number was " + number);
  3. }
  4. if(guess > number){
  5. l1.setText("Too high");
  6. }
  7. if(guess < number){
  8. l1.setText("Too low");
  9. }
  10.  
  11. public class Main {
  12. public static int number, guess;
  13. public static Random rand;
  14. public static Scanner scan;
  15. public static JButton b1 = new JButton("1");
  16. public static JButton b2 = new JButton("2");
  17. public static JButton b3 = new JButton("3");
  18. public static JButton b4 = new JButton("4");
  19. public static JButton b5 = new JButton("5");
  20. public static JButton b6 = new JButton("6");
  21. public static JButton b7 = new JButton("7");
  22. public static JButton b8 = new JButton("8");
  23. public static JButton b9 = new JButton("9");
  24. public static JButton b10 = new JButton("10");
  25. public static JLabel l1 = new JLabel("Guess a number between 1 and 10!");
  26.  
  27.  
  28. public Main(){
  29. frame();
  30. }
  31.  
  32. public void frame(){
  33. rand = new Random();
  34. number = rand.nextInt(10);
  35.  
  36. JFrame f = new JFrame();
  37. f.setResizable(false);
  38. f.setSize(500,500);
  39. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. f.setVisible(true);
  41. JPanel p = new JPanel();
  42. f.add(p);
  43. p.add(b1);
  44. p.add(b2);
  45. p.add(b3);
  46. p.add(b4);
  47. p.add(b5);
  48. p.add(b6);
  49. p.add(b7);
  50. p.add(b8);
  51. p.add(b9);
  52. p.add(b10);
  53. p.add(l1);
  54.  
  55. b1.addActionListener(new ActionListener(){
  56. public void actionPerformed(ActionEvent e){
  57. guess = 1;
  58. if(guess == number){
  59. l1.setText("You have won the number was " + number);
  60. }
  61. if(guess > number){
  62. l1.setText("Too high");
  63. }
  64. if(guess < number){
  65. l1.setText("Too low");
  66. }
  67. }
  68. });
  69.  
  70. b2.addActionListener(new ActionListener(){
  71. public void actionPerformed(ActionEvent e){
  72. guess = 2;
  73.  
  74. if(guess == number){
  75. l1.setText("You have won the number was " + number);
  76. }
  77. if(guess > number){
  78. l1.setText("Too high");
  79. }
  80.  
  81. if(guess < number){
  82. l1.setText("Too low");
  83. }
  84. }
  85. });
  86.  
  87. b3.addActionListener(new ActionListener(){
  88. public void actionPerformed(ActionEvent e){
  89. guess = 3;
  90.  
  91. if(guess == number){
  92. l1.setText("You have won the number was " + number);
  93. }
  94. if(guess > number){
  95. l1.setText("Too high");
  96. }
  97.  
  98. if(guess < number){
  99. l1.setText("Too low");
  100. }
  101. }
  102. });
  103.  
  104. b4.addActionListener(new ActionListener(){
  105. public void actionPerformed(ActionEvent e){
  106. guess = 4;
  107.  
  108. if(guess == number){
  109. l1.setText("You have won the number was " + number);
  110. }
  111. if(guess > number){
  112. l1.setText("Too high");
  113. }
  114.  
  115. if(guess < number){
  116. l1.setText("Too low");
  117. }
  118. }
  119. });
  120.  
  121. b5.addActionListener(new ActionListener(){
  122. public void actionPerformed(ActionEvent e){
  123. guess = 5;
  124.  
  125. if(guess == number){
  126. l1.setText("You have won the number was " + number);
  127. }
  128. if(guess > number){
  129. l1.setText("Too high");
  130. }
  131.  
  132. if(guess < number){
  133. l1.setText("Too low");
  134. }
  135. }
  136. });
  137.  
  138. b6.addActionListener(new ActionListener(){
  139. public void actionPerformed(ActionEvent e){
  140. guess = 6;
  141.  
  142. if(guess == number){
  143. l1.setText("You have won the number was " + number);
  144. }
  145. if(guess > number){
  146. l1.setText("Too high");
  147. }
  148.  
  149. if(guess < number){
  150. l1.setText("Too low");
  151. }
  152. }
  153. });
  154.  
  155. b7.addActionListener(new ActionListener(){
  156. public void actionPerformed(ActionEvent e){
  157. guess = 7;
  158.  
  159.  
  160. if(guess == number){
  161. l1.setText("You have won the number was " + number);
  162. }
  163. if(guess > number){
  164. l1.setText("Too high");
  165. }
  166.  
  167. if(guess < number){
  168. l1.setText("Too low");
  169. }
  170. }
  171. });
  172.  
  173. b8.addActionListener(new ActionListener(){
  174. public void actionPerformed(ActionEvent e){
  175. guess = 8;
  176.  
  177. if(guess == number){
  178. l1.setText("You have won the number was " + number);
  179. }
  180. if(guess > number){
  181. l1.setText("Too high");
  182. }
  183.  
  184. if(guess < number){
  185. l1.setText("Too low");
  186. }
  187. }
  188. });
  189.  
  190. b9.addActionListener(new ActionListener(){
  191. public void actionPerformed(ActionEvent e){
  192. guess = 9;
  193.  
  194. if(guess == number){
  195. l1.setText("You have won the number was " + number);
  196. }
  197. if(guess > number){
  198. l1.setText("Too high");
  199. }
  200.  
  201. if(guess < number){
  202. l1.setText("Too low");
  203. }
  204. }
  205. });
  206.  
  207. b10.addActionListener(new ActionListener(){
  208. public void actionPerformed(ActionEvent e){
  209. guess = 10;
  210.  
  211.  
  212. if(guess == number){
  213. l1.setText("You have won the number was " + number);
  214. }
  215. if(guess > number){
  216. l1.setText("Too high");
  217. }
  218.  
  219. if(guess < number){
  220. l1.setText("Too low");
  221. }
  222. }
  223. });
  224. }
  225.  
  226. public static void main(String[] args){
  227. new Main();
  228. }
  229. }
  230.  
  231. public static void guess(int guessNumber) {
  232. if (guessNumber == number) {
  233. l1.setText("You have won the number was " + number);
  234. }
  235. if (guessNumber > number) {
  236. l1.setText("Too high");
  237. }
  238.  
  239. if (guessNumber < number) {
  240. l1.setText("Too low");
  241. }
  242. }
  243.  
  244. b10.addActionListener(new ActionListener() {
  245. @Override
  246. public void actionPerformed(ActionEvent e) {
  247. guess(10);
  248. }
  249. });
  250.  
  251. b1.addActionListener(new MyActionListener(1))
  252. b1.addActionListener(new MyActionListener(2))
  253. ...
  254.  
  255. class MyActionListener implements ActionListener {
  256.  
  257. private int guess ;
  258. public MyActionListener(int guess){
  259. this.guess = guess;
  260. }
  261. @Override
  262. public void actionPerformed(ActionEvent e) {
  263. if(guess == number){
  264. l1.setText("You have won the number was " + number);
  265. }
  266. if(guess > number){
  267. l1.setText("Too high");
  268. }
  269.  
  270. if(guess < number){
  271. l1.setText("Too low");
  272. }
  273.  
  274. }
  275.  
  276. }
  277.  
  278. ActionListener listner = new ActionListener(){
  279. @Override
  280. public void actionPerformed(ActionEvent e){
  281. switch(e.getSource()){
  282. case b1:
  283. guess(1);
  284. break;
  285. case b2:
  286. guess(2);
  287. break;
  288. ...
  289. }
  290. }
  291. }
  292.  
  293. private void guess(int guess){
  294. if(guess == number){
  295. l1.setText("You have won the number was " + number);
  296. }
  297. if(guess > number){
  298. l1.setText("Too high");
  299. }
  300. if(guess < number){
  301. l1.setText("Too low");
  302. }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement