Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- public class Main {
- public static int number, guess;
- public static Random rand;
- public static Scanner scan;
- public static JButton b1 = new JButton("1");
- public static JButton b2 = new JButton("2");
- public static JButton b3 = new JButton("3");
- public static JButton b4 = new JButton("4");
- public static JButton b5 = new JButton("5");
- public static JButton b6 = new JButton("6");
- public static JButton b7 = new JButton("7");
- public static JButton b8 = new JButton("8");
- public static JButton b9 = new JButton("9");
- public static JButton b10 = new JButton("10");
- public static JLabel l1 = new JLabel("Guess a number between 1 and 10!");
- public Main(){
- frame();
- }
- public void frame(){
- rand = new Random();
- number = rand.nextInt(10);
- JFrame f = new JFrame();
- f.setResizable(false);
- f.setSize(500,500);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setVisible(true);
- JPanel p = new JPanel();
- f.add(p);
- p.add(b1);
- p.add(b2);
- p.add(b3);
- p.add(b4);
- p.add(b5);
- p.add(b6);
- p.add(b7);
- p.add(b8);
- p.add(b9);
- p.add(b10);
- p.add(l1);
- b1.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 1;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- b2.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 2;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- b3.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 3;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- b4.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 4;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- b5.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 5;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- b6.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 6;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- b7.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 7;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- b8.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 8;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- b9.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 9;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- b10.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- guess = 10;
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- });
- }
- public static void main(String[] args){
- new Main();
- }
- }
- public static void guess(int guessNumber) {
- if (guessNumber == number) {
- l1.setText("You have won the number was " + number);
- }
- if (guessNumber > number) {
- l1.setText("Too high");
- }
- if (guessNumber < number) {
- l1.setText("Too low");
- }
- }
- b10.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- guess(10);
- }
- });
- b1.addActionListener(new MyActionListener(1))
- b1.addActionListener(new MyActionListener(2))
- ...
- class MyActionListener implements ActionListener {
- private int guess ;
- public MyActionListener(int guess){
- this.guess = guess;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
- }
- ActionListener listner = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e){
- switch(e.getSource()){
- case b1:
- guess(1);
- break;
- case b2:
- guess(2);
- break;
- ...
- }
- }
- }
- private void guess(int guess){
- if(guess == number){
- l1.setText("You have won the number was " + number);
- }
- if(guess > number){
- l1.setText("Too high");
- }
- if(guess < number){
- l1.setText("Too low");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement