Home > Java > javaTutorial > Java GUI visualization example analysis

Java GUI visualization example analysis

PHPz
Release: 2023-05-17 08:04:19
forward
1545 people have browsed it

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.

<code>   // 初始化数据<br>   money = new int[100];<br>   for(int i = 0 ; i < money.length ; i ++)<br/>       money[i] = 100;</code>
Copy after login

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

,

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

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template