mirror of https://github.com/alibaba/arthas.git
add demo module. #260
parent
41936aed83
commit
aee4cde690
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.taobao.arthas</groupId>
|
||||
<artifactId>arthas-all</artifactId>
|
||||
<version>3.0.5-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>arthas-demo</artifactId>
|
||||
<name>arthas-demo</name>
|
||||
|
||||
<build>
|
||||
<finalName>arthas-demo</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
<showDeprecation>true</showDeprecation>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>demo.MathGame</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,63 @@
|
||||
package demo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class MathGame {
|
||||
private static Random random = new Random();
|
||||
|
||||
private int illegalArgumentCount = 0;
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
MathGame game = new MathGame();
|
||||
while (true) {
|
||||
game.run();
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
public void run() throws InterruptedException {
|
||||
try {
|
||||
int number = random.nextInt();
|
||||
List<Integer> primeFactors = primeFactors(number);
|
||||
print(number, primeFactors);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println(String.format("illegalArgumentCount:%3d, ", illegalArgumentCount) + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void print(int number, List<Integer> primeFactors) {
|
||||
StringBuffer sb = new StringBuffer(number + "=");
|
||||
for (int factor : primeFactors) {
|
||||
sb.append(factor).append('*');
|
||||
}
|
||||
if (sb.charAt(sb.length() - 1) == '*') {
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
}
|
||||
System.out.println(sb);
|
||||
}
|
||||
|
||||
public List<Integer> primeFactors(int number) {
|
||||
if (number < 2) {
|
||||
illegalArgumentCount++;
|
||||
throw new IllegalArgumentException("number is: " + number + ", need >= 2");
|
||||
}
|
||||
|
||||
List<Integer> result = new ArrayList<Integer>();
|
||||
int i = 2;
|
||||
while (i <= number) {
|
||||
if (number % i == 0) {
|
||||
result.add(i);
|
||||
number = number / i;
|
||||
i = 2;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue