马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
魚c們,能幫我看看我寫得好不好嘛
package com.systex.a507.homework.main;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class Lotto {
public static void main(String[] args) {
Set<String> numberSet = new TreeSet<>();
int group = 0;
try {
group = Integer.parseInt(args[0]);
} catch (NumberFormatException nfe) {
System.out.println("勿輸入非法字元, 請您輸入數字喔 ><' ");
}
while (numberSet.size() < group) {
List<Integer> numbers = generateGeneralNumbers();
int specialNumber = generateSpecialNumber();
// 確保特別號不與一般號碼重複
while (numbers.contains(specialNumber)) {
specialNumber = generateSpecialNumber();
}
// 對一般號碼做小到大排序
Collections.sort(numbers);
StringBuilder sb = new StringBuilder();
for (int number : numbers) {
sb.append(String.format("%02d ", number));
}
sb.append(String.format("特別號: %02d", specialNumber));
String result = sb.toString().replaceAll("[\\[\\,\\]]", "");
numberSet.add(result);
}
numberSet.forEach(System.out::println);
}
private static List<Integer> generateGeneralNumbers() {
List<Integer> numbers = new ArrayList<>();
Random random = new Random();
while (numbers.size() < 5) {
int num = random.nextInt(49) + 1;
if (!numbers.contains(num)) {
numbers.add(num);
}
}
return numbers;
}
private static int generateSpecialNumber() {
Random random = new Random();
return random.nextInt(49) + 1;
}
}
|