開始練習之前要注意的是:
a)爲項目取一個合適的包名稱.例如:package ljy.mytest.lotteryselecting;
b) 儘量一個JAVA 文件一個類,這樣在以後的查詢中也會一目瞭然,看一眼就知道這個類裏面是什麼內容。
c) 儘量爲每一個方法都寫上詳細的註釋:
例如:
/**
* 選出中獎綵球
* @param time 要選多少注彩票
* @param total 一共有多少的彩票球
* @param aim 一共要選出多少個彩票球
* @return 返回保存選中彩球結果的數組
*/
eclipse中在函數頭部添加說明性註釋和參數的註釋:
1)你在方法前輸入/**然後一回車,自動幫你生成方法和參數的註釋。
2)或者你在創建方法時,勾選生成註釋的選項,也能幫你生成。
3)要把鼠標焦點放在方法裏,然後ALT+SHIFT+J
d)保證main()方法中儘量的簡潔明瞭,這就需要你對方法的命名要言簡意賅,不要忘記遵守“駝峯命名法”。
e) 要保證你寫的方法的可擴展性,不要將其寫死了,這也是減少你工作量最主要的方法之一。
題目:
2. 寫一個36選7機選程序(如果有一天變爲1000選 99 怎麼辦?)
機選注數: 1(如果有一天變成選100注又怎麼辦 ?)
4 12 14 18 21 32 35 (儘量按從小到大順序輸出)。
/**
*
*/
package ljy.mytest.lotteryselecting;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
/**
* @author fshxxxyydys
*
*/
public class LotterySelecting { //選彩票的小程序
/**
* @param args
*/
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int total,aim,time;//總球數,要選球數,注數
System.out.print("請輸入注數:");
time = scanner.nextInt();
System.out.print("請輸入總球數:");
total = scanner.nextInt();
System.out.print("請輸入選球個數:");
aim = scanner.nextInt();
boolean sorted = isSorted();//判斷是否需要排序
printResults(time,total,aim,sorted); //打印結果
}
/**
* 確認是否要對選票結果進行排序
* @return 返回是否排序的確認結果
*/
private static boolean isSorted(){
System.out.print("請確認結果是否排序('y'OR'n'):");
String str = scanner.nextLine();
if(str.length() == 0){ //輸入回車鍵處理
str = scanner.nextLine();
}
if(str.length() == 1){
if(str.equalsIgnoreCase("n"))
return false;
else if(str.equalsIgnoreCase("y"))
return true;
else {
System.out.println("無法判斷,請重新輸入~");
isSorted();
}
}else {
System.out.println("無法判斷,請重新輸入~");
isSorted();
}
return false;
}
/**
* 選出中獎綵球
* @param time 要選多少注彩票
* @param total 一共有多少的彩票球
* @param aim 一共要選出多少個彩票球
* @return 返回保存選中彩球結果的數組
*/
private static int[] selectLottery ( int total ,int aim,boolean sorted){
int[] result = new int[aim];
Random r = new Random();
int temp ;
for(int i = 0 ; i < aim ; i++){
temp = 1 + r.nextInt(total); //注意:其中隨機數中不包含 0
if(!isContain(result, temp))
result[i] = temp;
else
i--; //保證選的數量足夠
}
if(sorted)
Arrays.sort(result);
return result;
}
/**
* 檢測是否已經存在目標綵球
* @param result 保存已經選好的綵球的數組
* @param target 要檢測是否已經存在數組的中的目標綵球
* @return 確認是否已存在
*/
private static boolean isContain(int[] result,int target){
for(int j = 0 ; j < result.length; j++)
if(result[j] == target)
return true;
return false;
}
/**
* 打印單次選彩票結果
* @param result 要打印的數組
*/
private static void printArrays(int[] result){
for(int x:result)
System.out.print(x +"\t");
System.out.println();
}
/**
* 打印選彩票的所有結果
* @param time 要選多少注彩票
* @param total 一共有多少的彩票球
* @param aim 一共要選出多少個彩票球
* @param sorted 是否要排序
*/
private static void printResults(int time,int total,int aim,boolean sorted){
for(int i = 0 ; i < time ; i++){
System.out.print("第"+ (i + 1) + "注:");
int[] temp = selectLottery(total,aim,sorted);
printArrays(temp);
}
}
}
====================================================================================
Result:
請輸入注數:7
請輸入總球數:36
請輸入選球個數:7
請確認結果是否排序('y'OR'n'):y
第1注:3 6
13 27 283136
第2注:7 17
19 23 242830
第3注:6 14
18 23 283033
第4注:2 3
7 15 182835
第5注:1 2
11 15 223135
第6注:4 5
12 24 252636
第7注:3 14
25 26 272829
====================================================================================