随机轮统计模拟java

v440hwme  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(334)

任务是模拟一个命运之轮,你可以转动十次。你可以随心所欲地旋转很多次,但是一旦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));
    }
}
eivnm1vs

eivnm1vs1#

要退出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

       }
}
bt1cpqcv

bt1cpqcv2#

我建议使用循环 do ... while 用于计数和打印。请检查你的代码-你需要计算相同的变量吗 anzahl1 对于每个 ges ?:

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

相关问题