2

I want to do a little lottery program for the school. You start with 500 credits, but every time you lose, 50 credits are deducted. My problem is that you always start with 500 credits again.

package jframe;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class lotto {
    public static void main(String[]args) {
        
        Random randI = new Random();
        

        
        
        
        JFrame frame = new JFrame("Lotto");
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        
        JTextField num1Field = new JTextField();
        num1Field.setBounds(80, 10, 100, 30);
        frame.add(num1Field);
        
        JTextField num2Field = new JTextField();
        num2Field.setBounds(80, 50, 100, 30);
        frame.add(num2Field);
        
        JTextField num3Field = new JTextField();
        num3Field.setBounds(80, 90, 100, 30);
        frame.add(num3Field);
        
        
        JLabel num1Label = new JLabel("Zahl 1: ");
        num1Label.setBounds(20, 10, 50, 30);
        frame.add(num1Label);

        JLabel num2Label = new JLabel("Zahl 2: ");
        num2Label.setBounds(20, 50, 50, 30);
        frame.add(num2Label);
        
        JLabel num3Label = new JLabel("Zahl 3: ");
        num3Label.setBounds(20, 90, 50, 30);
        frame.add(num3Label);
        
        JButton startButton = new JButton("Start!");
        startButton.setBounds(30, 150, 80, 30);
        frame.add(startButton);
        
        JButton resetButton = new JButton("Reset");
        resetButton.setBounds(120, 150, 80, 30);
        frame.add(resetButton);
        
        JLabel ergLabel = new JLabel();
        ergLabel.setBounds(10, 200, 400, 30);
        frame.add(ergLabel);
        
        JLabel ghLabel = new JLabel("500");
        ghLabel.setBounds(50, 230, 200, 30);
        frame.add(ghLabel);
        
        
        
        
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              
              int num1 = Integer.parseInt(num1Field.getText());
              int num2 = Integer.parseInt(num2Field.getText());
              int num3 = Integer.parseInt(num2Field.getText());
              

              
              int credit = 500;
              
           
              System.out.println(credit);
              
              
              
              int pcnum1 = randI.nextInt(48);
              pcnum1 = pcnum1+1;
              
              int pcnum2 = randI.nextInt(48);
              pcnum2 = pcnum2+1;
              
              int pcnum3 = randI.nextInt(48);
              pcnum3 = pcnum3+1;
              
              boolean zahl1 = false;
              boolean zahl2 = false;
              boolean zahl3 = false;
              
              if(num1 == pcnum1) {
                  zahl1 = true;
              } else {
                  zahl1 = false;
              }
              
              if(num2 == pcnum2) {
                  zahl2 = true;
              } else {
                  zahl2 = false;
              }
              
              if(num3 == pcnum3) {
                  zahl3 = true;
              } else {
                  zahl3 = false;
              }
              
              if(zahl1 == true && zahl2 == true && zahl3 == true) {
                  credit = credit + 500;
              }
              
              if(zahl1 == true && zahl2 == true && zahl3 == false || zahl1 == true && zahl3 == true && -                 zahl2 == false || zahl2 == true && zahl3 == true && zahl1 == false) {
                  credit = credit + 250;
              }
              
              if(zahl1 == true && zahl2 == false && zahl3 == false || zahl1 == false && zahl3 == false -                 && zahl2 == true || zahl1 == false && zahl2 == false && zahl3 == true) {
                  credit = credit + 100;
              }
              
              if(zahl1 == false && zahl2 == false && zahl3 == false){
                  credit = credit - 50;
              }
              
              ergLabel.setText("1. Number: " + zahl1 + "  2. Number: " + zahl2 + "  3. Number: " +          -                 zahl3);
              ghLabel.setText("Credit: " + credit);
              
            }
          });
        
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                
                num1Field.setText("");
                num2Field.setText("");
                num3Field.setText("");
                ergLabel.setText("");
            }
          });
        
        

        
        
        
        
        
        frame.setVisible(true);
        
    }
}

My only solution would be to declare the variable outside the function, but I don't know how to use the variable inside the function if I declare it outside.

PsydoV2
  • 23
  • 3

1 Answers1

0
  1. Move int credit = 500; to be an instance variable
  2. Move all your code in main into a default constructor for your class Lotto
  3. Create an instance of Lotto in your main method

Also it is convention to capitalize class names so I changed that here.

public class Lotto {
    
    int credit = 500;

    public Lotto() 
    {
        Random randI = new Random();
        
        JFrame frame = new JFrame("Lotto");
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        // ...
    }
    
    public static void main(String[]args) {
        Lotto lotto = new Lotto();
    }

Or if you want to keep your code in main and no instances created, move int credit = 500; out of the method and make it static.

public class Lotto {
    static int credit = 500;

If you make variables in a class static, then you don't have to have an instance to use it.
If you make variables in a class not static, then you have to have an instance Lotto lotto = new Lotto(); to access it

buffman23
  • 36
  • 3