财富之轮统计模拟java

财富之轮统计模拟java,java,random,statistics,Java,Random,Statistics,任务是模拟一个命运之轮,你可以转动十次。 你可以随心所欲地旋转很多次,但是一旦0出现,所有的分数都消失了。当得分超过10分或达到0分时,程序应立即停止该轮。结果应添加到末尾 我们现在正处于点被添加,字段被固定的位置,但是我们想不出任何与停止或添加结果有关的事情 有人有主意吗? 提前谢谢 import java.util.Map; import java.util.LinkedHashMap; public class RandomBeispielzwei { private stat

任务是模拟一个命运之轮,你可以转动十次。 你可以随心所欲地旋转很多次,但是一旦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));
    }
}
import java.util.Map;
导入java.util.LinkedHashMap;
贝斯皮尔茨韦公共课{
私有静态最终映射GRENZEN=newlinkedhashmap();
静止的{
GRENZEN.put(0.1,1);
格雷森.普特(0.2,2);
格雷森.普特(0.3,3);
格伦森.普特(0.4,1);
格伦森.普特(0.5,2);
格伦森.普特(0.6,3);
格伦森.普特(0.7,1);
格雷森.普特(0.8,2);
格伦森.普特(0.9,3);
格雷森.普特(1.0,0);
}
私有整数naechsteZufallzahl(){
double random=Math.random();
对于(Map.Entry:GRENZEN.entrySet()){
if(random用于退出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
    
       }
}
import java.util.Map;
导入java.util.LinkedHashMap;
贝斯皮尔茨韦公共课{
私有静态最终映射GRENZEN=newlinkedhashmap();
静止的{
GRENZEN.put(0.1,1);
格雷森.普特(0.2,2);
格雷森.普特(0.3,3);
格伦森.普特(0.4,1);
格伦森.普特(0.5,2);
格伦森.普特(0.6,3);
格伦森.普特(0.7,1);
格雷森.普特(0.8,2);
格伦森.普特(0.9,3);
格雷森.普特(1.0,0);
}
私有整数naechsteZufallzahl(){
double random=Math.random();
对于(Map.Entry:GRENZEN.entrySet()){
if(random=10)break;//到目前为止至少得到了10分,因此我们退出for循环
}
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”);
系统输出打印LN(“Gesamtzahl:+(anzahl1+anzahl2+anzahl3+anzahl0));
System.out.println(“Gesamtpunktzahl:+Gesamtpunktzahl);//既然我们已经计算了它,我们还是在这里使用它为好
}
}

我建议使用循环
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);


publicstaticvoidmain(字符串[]args){
int[]anzahles=新int[4];
int gesamtpunktzahl=0;
整数z=0;
RandomBeispielzwei b=新的RandomBeispielzwei();
做{
z=b.naechsteZufallzahl();
anzahles[z]++;
gesamtpunktzahl+=z;
}而(!(z==0 | | gesamtpunktzahl>=10));
int ges[]=新int[3];
对于(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());
}

如果你说如果
z==0
sum==10
你需要立即停止旋转,为什么要重复旋转10000次?问题是-旋转的总和?键,还是地图的值?
    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());
    }