Java :: Aufgabe #14
3 Lösungen

Lotto - Simulation - 6 aus 49
Anfänger - Java
von Gustl
- 25.11.2012 um 16:16 Uhr
Schreiben Sie ein Programm, welches 6 Zahlen zwischen 1 und 49 abfrägt. Danach soll das Programm solange 6 Zufallszahlen berechnen bis alle 6 eingegebenen Zahlen übereinstimmen. Natürlich darf eine Zahl nicht zweimal vorkommen.
Dann soll ausgegeben werden nach wie vielen "Spielen" ein 6er erzielt wurde und wie hoch die Chance auf einen 3er, einen 4er und einen 5er war.
Dann soll ausgegeben werden nach wie vielen "Spielen" ein 6er erzielt wurde und wie hoch die Chance auf einen 3er, einen 4er und einen 5er war.
Lösungen:

public class lotto { private static double spiele; private static int enull,eeins,ezwei,edrei,evier,efuenf,esechs; private static int eins,zwei,drei,vier,fuenf,sechs; private static int count = 0; private static int[] lottozahlen = new int[50]; public static void main(String[] args) { setzezahlen(5,10,15,20,25,30); while (count != 6){ count = 0; zufallszahlen(); spiel(); verteilung(); spiele++; } ausgabe(); } public static void setzezahlen(int a,int b,int c, int d,int e,int f){ lottozahlen[a] = 1; lottozahlen[b] = 1; lottozahlen[c] = 1; lottozahlen[d] = 1; lottozahlen[e] = 1; lottozahlen[f] = 1; } public static void zufallszahlen(){ eins = (int)(Math.random()*49+1); do{ zwei = (int)(Math.random()*49+1); } while (zwei == eins); do{ drei = (int)(Math.random()*49+1); } while (drei == eins || drei == zwei); do{ vier = (int)(Math.random()*49+1); } while (vier == eins || vier == zwei || vier == drei); do{ fuenf = (int)(Math.random()*49+1); } while (fuenf == eins || fuenf == zwei || fuenf == drei || fuenf == vier); do{ sechs = (int)(Math.random()*49+1); } while (sechs == eins || sechs == zwei || sechs == drei || sechs == vier || sechs == fuenf); } public static boolean pruefe(int x){ boolean c = false; if(lottozahlen[x] == 1){ c = true; } return c; } public static void ergebnis(){ System.out.print(count+ " aus 49."); } public static void verteilung(){ if (count == 0) enull++; if (count == 1) eeins++; if (count == 2) ezwei++; if (count == 3) edrei++; if (count == 4) evier++; if (count == 5) efuenf++; if (count == 6) esechs++; } public static void ausgabe(){ double etemp = (int)enull; double prozent = etemp*100/spiele; System.out.println("Spiele: "+(int)spiele); System.out.println("0 richtige: "+enull+ " Chance: "+prozent+"%"); etemp = (int)eeins; prozent = etemp*100/spiele; System.out.println("1 richtige: "+eeins+ " Chance: "+prozent+"%"); etemp = (int)ezwei; prozent = etemp*100/spiele; System.out.println("2 richtige: "+ezwei+ " Chance: "+prozent+"%"); etemp = (int)edrei; prozent = etemp*100/spiele; System.out.println("3 richtige: "+edrei+ " Chance: "+prozent+"%"); etemp = (int)evier; prozent = etemp*100/spiele; System.out.println("4 richtige: "+evier+ " Chance: "+prozent+"%"); etemp = (int)efuenf; prozent = etemp*100/spiele; System.out.println("5 richtige: "+efuenf+ " Chance: "+prozent+"%"); etemp = (int)esechs; prozent = etemp*100/spiele; System.out.println("6 richtige: "+esechs+ " Chance: "+prozent+"%"); } public static void spiel(){ if (pruefe(eins)== true) count++; if (pruefe(zwei)== true) count++; if (pruefe(drei)== true) count++; if (pruefe(vier)== true) count++; if (pruefe(fuenf)== true) count++; if (pruefe(sechs)== true) count++; } }

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; public class mainClass { static int[] array1 = new int[6]; static int[] array2 = new int[6]; static int anzahl; static JLabel labVersuche; static JLabel labAnzVersuche; static JLabel labTreffDrei; static JLabel labTreffVier; static JLabel labTreffFuenf; static JLabel labDreiProz; static JLabel labVierProz; static JLabel labFuenfProz; static JButton btnRand; public static int drei; public static int vier; public static int fuenf; public static int sechs; static int counter; static void arrCmp(int[] arr1, int[] arr2){ for(int i = 0; i < 6; i++) { for(int j = 0; j < 6; j++) { if(arr1[i] == arr2[j]) counter++; } } if(counter == 3) drei++; else if(counter == 4) vier++; else if(counter == 5) fuenf++; else if(counter > 5) sechs++; counter=0; } static int[] randoms(int menge, int range) { int[] a = new int[menge]; List<Integer> zahlen = new ArrayList<Integer>(); for(int i = 1; i < range+1; i++) { zahlen.add(i); } Random rand = new Random(); for(int i = 0; i < menge; i++) { int zahlenIndex = rand.nextInt(zahlen.size()); a[i] = zahlen.get(zahlenIndex); zahlen.remove(zahlenIndex); } Arrays.sort(a); return a; } public static void main(String[] args) { final JFrame ew = new JFrame("Lottozahlen"); ew.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ew.setLocationRelativeTo(null); ew.setMinimumSize(new Dimension(500, 300)); ew.setLayout(new BorderLayout()); JPanel panNorth = new JPanel(new GridLayout(0, 1)); JLabel labHinweis = new JLabel("Wieviel Versuche bis zu den 6 Richtigen!"); labHinweis.setHorizontalAlignment(SwingConstants.CENTER); panNorth.add(labHinweis); ew.add(panNorth,BorderLayout.NORTH); JPanel panCenter = new JPanel(new GridLayout(0, 2)); final JButton btnTipp = new JButton("Tippen"); btnTipp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { array1 = randoms(6, 49).clone(); String txtTipp = Arrays.toString(array1); btnTipp.setText(txtTipp); } }); btnRand = new JButton("Ziehung"); btnRand.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { while(sechs < 1) { array2 = randoms(6, 49).clone(); arrCmp(array1, array2); anzahl++; labAnzVersuche.setText(Integer.toString(anzahl)); labAnzVersuche.paintImmediately(labAnzVersuche.getVisibleRect()); labTreffDrei.setText("3er: "+drei); labTreffDrei.paintImmediately(labTreffDrei.getVisibleRect()); labTreffVier.setText("4er: "+vier); labTreffVier.paintImmediately(labTreffVier.getVisibleRect()); labTreffFuenf.setText("5er: "+fuenf); labTreffFuenf.paintImmediately(labTreffFuenf.getVisibleRect()); labDreiProz.setText(((double)(100)* (double) (drei)/ (double) (anzahl))+"%"); labDreiProz.paintImmediately(labDreiProz.getVisibleRect()); labVierProz.setText(((double)(100)* (double) (vier)/ (double) (anzahl))+"%"); labVierProz.paintImmediately(labVierProz.getVisibleRect()); labFuenfProz.setText(((double)(100)* (double) (fuenf)/ (double) (anzahl))+"%"); labFuenfProz.paintImmediately(labFuenfProz.getVisibleRect()); } labVersuche.setText("Hurra! 6 Richtige nach "); labVersuche.setHorizontalAlignment(SwingConstants.RIGHT); labVersuche.paintImmediately(labVersuche.getVisibleRect()); labAnzVersuche.setText(Integer.toString(anzahl)+" Versuchen"); labAnzVersuche.paintImmediately(labAnzVersuche.getVisibleRect()); } }); panCenter.add(btnTipp); panCenter.add(btnRand); ew.add(panCenter,BorderLayout.CENTER); JPanel panSouth = new JPanel(new GridLayout(0, 2)); labVersuche = new JLabel("Versuche: "); labAnzVersuche = new JLabel("Anzahl"); JPanel panSouthLeft = new JPanel(new GridLayout(0, 1)); JPanel panSouthRight = new JPanel(new GridLayout(0, 1)); JLabel labTreffer = new JLabel("Treffer:"); labTreffDrei = new JLabel("3er:"); labTreffVier = new JLabel("4er:"); labTreffFuenf = new JLabel("5er:"); JLabel labDummy = new JLabel(""); labDreiProz = new JLabel(""); labVierProz = new JLabel(""); labFuenfProz = new JLabel(""); panSouthLeft.add(labTreffer); panSouthLeft.add(labTreffDrei); panSouthLeft.add(labTreffVier); panSouthLeft.add(labTreffFuenf); panSouthRight.add(labDummy); panSouthRight.add(labDreiProz); panSouthRight.add(labVierProz); panSouthRight.add(labFuenfProz); panSouth.add(labVersuche); panSouth.add(labAnzVersuche); panSouth.add(panSouthLeft); panSouth.add(panSouthRight); ew.add(panSouth,BorderLayout.SOUTH); ew.setVisible(true); } }

Console.Out.WriteLine("Willkommen Beim Lotto!!... nenne mir jetzt Deine 6 Glückszahlen zwischen 1 und 49"); int[] spielfeld = new int[6]; for(int i = 0; i < 6; i++){ Console.Out.WriteLine("Zahl " + (i+1) + ".: "); spielfeld[i] = int.Parse(Console.In.ReadLine()); } Random random = new Random(); int counter = 0; int[] lottofeld = new int[6]; int c = 0; do{ int hilfsint = 0; int[] mehrfach = new int[6]; c = 0; for(int i = 0; i < 6; i++){ do{hilfsint = random.Next(1,49);} while(hilfsint == mehrfach[0] || hilfsint == mehrfach[1] || hilfsint == mehrfach[2] || hilfsint == mehrfach[3] || hilfsint == mehrfach[4] || hilfsint == mehrfach[5]); mehrfach[i] = hilfsint; lottofeld[i] = mehrfach[i]; if(lottofeld[i] == spielfeld[0] || lottofeld[i] == spielfeld[1] || lottofeld[i] == spielfeld[2] || lottofeld[i] == spielfeld[3] || lottofeld[i] == spielfeld[4] || lottofeld[i] == spielfeld[5]){ c = c + 1; } } Console.Out.WriteLine(lottofeld[0] + " - " + lottofeld[1] + " - " + lottofeld[2] + " - " + lottofeld[3] + " - " + lottofeld[4] + " - " + lottofeld[5] + " du hast: " + c + " Richtige..!"); counter = counter + 1; } while(c != 6); Console.Out.WriteLine("Du hast: " + counter + " Spiele Benötigt!"); Console.Out.WriteLine("Deine gespielten Zahlen: " + spielfeld[0] + " - " + spielfeld[1] + " - " + spielfeld[2] + " - " + spielfeld[3] + " - " + spielfeld[4] + " - " + spielfeld[5] + " ! "); }