Lottozahlen mit grafischer Oberfläche

meinnameist

Mitglied
Hallo Leute! Ich hab mal wieder n kleines Problemchen..

Undzwar soll ich die Lottozahlen ausgeben und verhindern, dass gleiche Zahlen erscheinen. Jedoch wird bei mir immer nur eine Zahl ausgegeben und ich komme grad nicht dahinter, weshalb!

Wäre schön, wenn mir jemand nen Hinweis geben könnte 🙂


Code:
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTextArea;

public class Lotto extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JButton jButton = null;
	private JTextField jTextField = null;
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(120, 77, 152, 31));
			jButton.setHorizontalTextPosition(SwingConstants.CENTER);
			jButton.setText("Ausgeben");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
					int [] zufallszahlen = new int [6];
					for(int i=0; i<zufallszahlen.length; i++){
						zufallszahlen[i]= 1+(int)(49*Math.random());
					}
					for(int i=0; i<zufallszahlen.length; i++){
						for(int k=0; k<zufallszahlen.length;k++){
							if(i!=k){
								zufallszahlen[i]=1+(int)(49*Math.random());							
							}
							
						}
						jTextField.setText(zufallszahlen[i]+"");	
					}

				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(45, 120, 302, 137));
		}
		return jTextField;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Lotto thisClass = new Lotto();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public Lotto() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(400, 300);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(45, 15, 301, 33));
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setHorizontalTextPosition(SwingConstants.CENTER);
			jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
			jLabel.setText("Lottozahlen");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJTextField(), null);
		}
		return jContentPane;
	}

}  //  @jve:decl-index=0:visual-constraint="294,30"
 
Es geht viel einfacher: Fülle eine Liste mit den Zahlen 1 bis 49, rufe java.util.Collections.shuffle(list) auf und nimm die ersten 6 Zahlen.
 
Wir sollen schon mit Arrays und for schleifen arbeiten, aber danke trotzdem 🙂
Es wäre echt super, wenn du mir hierbei helfen würdest, und meine Fehler vll findest 🙂
 
Du kannst die shuffle-Lösung auch mit Arrays und for-Schleifen nachbauen:
Fülle ein Array mit den Zahlen von 1 bis 49
Wiederhole 500 mal:
index1 = Zufallszahl zwischen 0 und 48
index2 = Zufallszahl zwischen 0 und 48
vertausche Arrayinhalt von index1 und index2 (dazu braucht man eine Variable zum zwischenspeichern)

Nebenbei noch ein Tipp: Nimm nicht Math.random(), sondern erzeuge ein Random-Objekt, denn das hat die Methode Random.nextInt(k), was eine Zahl zwischen 0 und k-1 liefert.
 
Java:
						if (i==0) 
							jTextField.setText(zufallszahlen[i]+"");
						else 
							jTextField.setText(jTextField.getText() + " - " + zufallszahlen[i]+"");
du musst das was du schon im TextField stehen hast (die letzte(n) Zahl(en)) auch nochmal hinschreiben beim nächstn Durchlauf.
 
Zuletzt bearbeitet:
Collections.shuffle arbeitet letztlich auch mit einem Array, noch bisschen anders:
Java:
    /**
     * Randomly permute the specified list using the specified source of
     * randomness.  All permutations occur with equal likelihood
     * assuming that the source of randomness is fair.<p>
     *
     * This implementation traverses the list backwards, from the last element
     * up to the second, repeatedly swapping a randomly selected element into
     * the "current position".  Elements are randomly selected from the
     * portion of the list that runs from the first element to the current
     * position, inclusive.<p>
     *
     * This method runs in linear time.  If the specified list does not
     * implement the {@link RandomAccess} interface and is large, this
     * implementation dumps the specified list into an array before shuffling
     * it, and dumps the shuffled array back into the list.  This avoids the
     * quadratic behavior that would result from shuffling a "sequential
     * access" list in place.
     *
     * @param  list the list to be shuffled.
     * @param  rnd the source of randomness to use to shuffle the list.
     * @throws UnsupportedOperationException if the specified list or its
     *         list-iterator does not support the <tt>set</tt> operation.
     */
    public static void shuffle(List<?> list, Random rnd) {
        int size = list.size();
        if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
            for (int i=size; i>1; i--)
                swap(list, i-1, rnd.nextInt(i));
        } else {
            Object arr[] = list.toArray();

            // Shuffle array
            for (int i=size; i>1; i--)
                swap(arr, i-1, rnd.nextInt(i));

            // Dump array back into list
            ListIterator it = list.listIterator();
            for (int i=0; i<arr.length; i++) {
                it.next();
                it.set(arr[i]);
            }
        }
    }
 
Zuletzt bearbeitet von einem Moderator:

Zurück
Oben