mercredi 18 novembre 2020

Wheel of fortune statistical simulation java

The task is to simulate a wheel of fortune, which you are allowed to turn ten times. You can spin as many times as you like, but as soon as the 0 comes, all points are gone. The program should stop the round as soon as a score over 10 is reached or a 0 comes. The results should be added at the end.

We are now at the point where the points are added and fields are fixed, but we can't think of anything to do with stopping or adding the results.

Does anyone have an idea? Thanks in advance!

import java.util.Map;
import java.util.LinkedHashMap;

public class RandomBeispielzwei {

    private static final Map<Double, Integer> GRENZEN = new LinkedHashMap<Double, Integer>();

    static {
      GRENZEN.put(0.1, 1);
      GRENZEN.put(0.2, 2);
      GRENZEN.put(0.3, 3);
      GRENZEN.put(0.4, 1);
      GRENZEN.put(0.5, 2);
      GRENZEN.put(0.6, 3);
      GRENZEN.put(0.7, 1);
      GRENZEN.put(0.8, 2);
      GRENZEN.put(0.9, 3);
      GRENZEN.put(1.0, 0); 
    }

    private Integer naechsteZufallzahl() {
      double random = Math.random();
      for (Map.Entry<Double, Integer> entry : GRENZEN.entrySet()) {
       if (random <= entry.getKey().doubleValue()) {
            return entry.getValue();
        }
      }

      throw new UnsupportedOperationException("Fuer die Zufallszahl wurde kein passender Wert in der Map gefunden");
    }

    public static void main(String[] args) {
      int anzahl1 = 0;
      int anzahl2 = 0;
      int anzahl3 = 0;
      int anzahl0 = 0;
      RandomBeispielzwei b = new RandomBeispielzwei();

      for (int i = 0; i < 10; i++) {
        
        Integer z = b.naechsteZufallzahl();
        if (z.intValue() == 1) {
            anzahl1++;
        } else if (z.intValue() == 2) {
            anzahl2++;
        } else if (z.intValue() == 3) {
            anzahl3++;
        } else {
            anzahl0++;
        }
      }
      int ges1 = anzahl1 * 1; 
      int ges2 = anzahl1 * 2;   
      int ges3 = anzahl1 * 3;
    
      System.out.println("1: " + anzahl1);
      System.out.println("Punktzahl 1: " + ges1);
      System.out.println("2: " + anzahl2);
      System.out.println("Punktzahl 2: " + ges2);
      System.out.println("3: " + anzahl3);
      System.out.println("Punktzahl 3: " + ges3);
      System.out.println("0: " + anzahl0);
      System.out.println("Gesamtzahl: " + (anzahl1 + anzahl2 + anzahl3 + anzahl0));
      System.out.println("Gesamtpunktzahl: " + (ges1 + ges2 + ges3));
    }
}



Aucun commentaire:

Enregistrer un commentaire