Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: imporve MathGame 加入sleep及改为多层调用以模拟实际情况,方便测试 #2718

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions math-game/src/main/java/demo/MathGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public static void main(String[] args) throws InterruptedException {
public void run() throws InterruptedException {
try {
int number = random.nextInt()/10000;
List<Integer> primeFactors = primeFactors(number);
List<Integer> primeFactors = primeFactorsByPass3(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) {
public static void print(final int number, final List<Integer> primeFactors) {
StringBuffer sb = new StringBuffer(number + "=");
for (int factor : primeFactors) {
sb.append(factor).append('*');
Expand All @@ -40,7 +40,24 @@ public static void print(int number, List<Integer> primeFactors) {
System.out.println(sb);
}

public List<Integer> primeFactorsByPass3(final int number) {
return primeFactorsByPass2(number);
}

public List<Integer> primeFactorsByPass2(final int number) {
return primeFactorsByPass1(number);
}

public List<Integer> primeFactorsByPass1(final int number) {
return primeFactors(number);
}

public List<Integer> primeFactors(int number) {
try {
Thread.sleep(5);
} catch (final InterruptedException ex) {
throw new IllegalStateException (ex);
}
if (number < 2) {
illegalArgumentCount++;
throw new IllegalArgumentException("number is: " + number + ", need >= 2");
Expand Down