Home  >  Article  >  Java  >  Java GUI visualization example analysis

Java GUI visualization example analysis

PHPz
PHPzforward
2023-05-17 08:04:191393browse

Question: There are 100 people in the room, each of them has 100 yuan, and they are playing a game. In each round of the game, everyone has to give one dollar to another person randomly. What is the wealth distribution of these 100 people in the end? Guess, after 10,000 exchanges, what do you think the final result will be like?

The answer is like this.


Java GUI可视化实例分析 Is it different from your intuitive idea? Did you think it was evenly distributed at first?

In fact, many people did not expect the result to be like this at the beginning.

We use Java GUI to visually understand this problem.

First initialize the data. At the beginning, everyone has 100 yuan.

   // 初始化数据
   money = new int[100];
   for(int i = 0 ; i < money.length ; i ++)
money[i] = 100;

Initial stateJava GUI可视化实例分析
Then in each round of the game, everyone must
take out one dollar and give it to another person randomly

,

 for(int i = 0 ; i < money.length; i ++){
if(money[i] > 0){
          int j = (int)(Math.random() * money.length);
          money[i] -= 1;
          money[j] += 1;
       }
  }
Java GUI可视化实例分析Not intuitive enough? Then we can sort first and then display.
Arrays.sort(money);
for(int i = 0 ; i < money.length; i ++){
if(money[i] > 0){
          int j = (int)(Math.random() * money.length);
          money[i] -= 1;
          money[j] += 1;
       }
  }

Java GUI可视化实例分析

The above is the detailed content of Java GUI visualization example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete