標題:
Lottery
[打印本頁]
作者:
buy
時間:
2010-10-23 14:11
標題:
Lottery
//樂透開獎-suck
int []lottery = new int[]{0,0,0};
//1
double rd = Math.random()*10+1; //隨機取1~10的倍精浮點數
int Result = (int)rd;
lottery[0] = Result;
//2
do{
rd = Math.random()*10+1;
Result = (int)rd;
lottery[1] = Result;
}while(lottery[0] == lottery[1]);
//3
do{
rd = Math.random()*10+1;
Result = (int)rd;
lottery[2] = Result;
}while((lottery[0] == lottery[2]) || (lottery[1] == lottery[2]));
System.out.println("三個開獎號為:"+lottery[0]+","+lottery[1]+","+lottery[2]);
作者:
buy
時間:
2010-10-23 14:25
//樂透開獎-good
double rd;//int Result;
int []lottery = new int[]{1,2,3,4,5,6,7,8,9,10};
int rdindex = 0,temp = 0;
for(int i=0 ; i<10 ; i++)
{
System.out.println(lottery[i]);
}
//隨機交換陣列值
for(int i=0 ; i<10 ; i++)
{
rd = Math.random()*10;
rdindex = (int)rd; //隨機取位置0~9
//兩個位置的值交換 lottery[i] lottery[?]
temp = lottery[i];
lottery[i] = lottery[rdindex];
lottery[rdindex] = temp;
}
//把整個lottery[]陣列列印出來
for(int i=0 ; i<10 ; i++)
{
System.out.println(lottery[i]);
}
System.out.println("三個開獎號為:"+lottery[0]+","+lottery[1]+","+lottery[2]);
複製代碼
作者:
buy
時間:
2010-10-23 14:45
[attach]41[/attach]
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2