【问题标题】:Wheel of fortune statistical simulation java命运之轮统计模拟java
【发布时间】:2021-03-01 13:14:34
【问题描述】:

任务是模拟一个命运之轮,你可以转动十次。 您可以随意旋转多次,但一旦 0 到来,所有点都消失了。一旦得分超过 10 或出现 0,程序应立即停止该回合。结果应该加在最后。

我们现在处于添加点和固定字段的地步,但我们想不出任何与停止或添加结果有关的事情。

有人有想法吗? 提前致谢!

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));
    }
}

【问题讨论】:

  • 如果z == 0sum == 10 说需要立即停止旋转,为什么还要重复旋转10000 次。问题是——总和?地图的键或值?

标签: java random statistics


【解决方案1】:

要退出 for 循环(和任何其他循环),您可以使用“break”语句,它只是结束循环(类似于“return”如何退出方法)。为了能够在总分达到 10 时停止,您当然需要跟踪总分。为此,最简单的方法是引入一个整数变量(例如“gesamtpunktzahl”),将每轮得分的数量添加到该变量中。总而言之,它看起来像这样:

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;
        int gesamtpunktzahl = 0;  // this will store what the total score is so far
        RandomBeispielzwei b = new RandomBeispielzwei();

  
        for (int i = 0; i < 10000; i++) {
            
            Integer z = b.naechsteZufallzahl();
            if (z.intValue() == 1) {
                anzahl1++;
                gesamtpunktzahl++; // a 1 was scored, so we increase the total score by 1
            } else if (z.intValue() == 2) {
                anzahl2++;
                gesamtpunktzahl += 2;  // same with a 2
            } else if (z.intValue() == 3) {
                anzahl3++;
                gesamtpunktzahl += 3;  // same with a 3
            } else {
                anzahl0++;
                break;  // a 0 was rolled, so we end the game (by exiting the for-loop)
            }
            
            if (gesamtpunktzahl >= 10) break;  // at least 10 points were scored so far, so we exit the for-loop
        }
        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: " + gesamtpunktzahl); // since we calculated it anyway, we might as well just use it here
    
       }
}

【讨论】:

    【解决方案2】:

    我建议使用循环do ... while 进行计数和打印。 请检查您的代码 - 您是否需要为每个 ges 计算相同的变量 anzahl1?:

      int ges1 = anzahl1 * 1; 
      int ges2 = anzahl1 * 2;   
      int ges3 = anzahl1 * 3;
    

    如果不是 - 则将 ges[i] = anzahles[1] * (i+1); 替换为 ges[i] = anzahles[i+1] * (i+1);


        public static void main(String[] args) {
    
            int[] anzahles = new int[4];
            int gesamtpunktzahl = 0;
            Integer z = 0;
            RandomBeispielzwei b = new RandomBeispielzwei();
    
            do {
                z = b.naechsteZufallzahl();
                anzahles[z]++;
                gesamtpunktzahl += z;
    
            } while (!(z == 0 || gesamtpunktzahl >= 10));
    
            int ges[] = new int[3];
            for (int i = 0; i < 3; i++) {
                ges[i] = anzahles[1] * (i+1);
                System.out.println((i+1) + ": " + anzahles[i+1]);
                System.out.println("Punktzahl " + (i+1) + ": " + ges[i]);
            }
    
            System.out.println("0: " + anzahles[0]);
            System.out.println("Gesamtzahl: " + Arrays.stream(anzahles).sum());
            System.out.println("Gesamtpunktzahl: " + Arrays.stream(ges).sum());
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 2022-01-18
      • 2016-10-25
      • 2019-07-21
      • 1970-01-01
      • 2012-11-25
      相关资源
      最近更新 更多