最近有在練習一些小題目
把這個寫得比較完整的寫出來
做一個紀錄
有錯誤的話再有勞指導一下了!!!!
--------------------------------------------------------------------------------------------
import java.io.*;
import java.util.ListIterator;
import java.util.Random;
public class loto {
static int loto[] = new int[6];
static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args)throws IOException {
System.out.print("自選=1,電腦選=2 您的選擇是 = "); //選擇要亂數產生還是自行輸入
String cho = buf.readLine();
int chon = Integer.parseInt(cho);
switch(chon)
{
case 1:
selfloto();
break;
case 2:
rndloto();
break;
default:
System.out.print("NO NO");
}
}
//自行輸入樂透數字
public static void selfloto()throws IOException{
for(int i = 0 ; i < loto.length ; i++)
{
System.out.print("請輸入"+(i+1)+"的號碼");
String chonn = buf.readLine();
loto[i] = Integer.parseInt(chonn);
if(loto[i]>=1&&loto[i]<=49) //判斷輸入數字是否在1~49內
{
for(int j = 0 ; j<i;j++) //判斷輸入數字是否有與前面重複
{
if(loto[j] == loto[i])
{
System.out.print("數字重複,重新輸入");
i--;
break;
}
}
}
else
{
System.out.print("數字錯誤,重新輸入");
i--;
}
}
//排序
for(int i = 0 ; i<loto.length; i++)
{
for(int j = i+1;j<loto.length;j++)
{
if(loto[i] > loto[j])
{ int tmp = loto[j];
loto[j] = loto[i];
loto[i] = tmp ;
}
}
}
for(int i = 0 ; i < loto.length;i++)
{
System.out.print(loto[i]+" ");
}
}
//隨機產生樂透數字
public static void rndloto()throws IOException
{
System.out.print("需要購買幾組 =");
String count = buf.readLine();
int countt = Integer.parseInt(count);
for(int num = 0 ; num < countt ; num++)
{
for(int i = 0 ; i<loto.length; i++)
{
loto[i] = (int) (Math.random()*49+1); //亂數產生1~49
for(int j = 0 ; j< i;j++) //
{
if(loto[i] == loto[j])
{
i--;
break;
}
}
}
//排序
for(int i = 0 ; i<loto.length; i++)
{
for(int j = i+1;j<loto.length;j++)
{
if(loto[i] > loto[j])
{ int tmp = loto[j];
loto[j] = loto[i];
loto[i] = tmp ;
}
}
}
System.out.print("第"+(num+1)+"組數字 = ");
for(int i = 0 ; i < loto.length;i++)
{
System.out.print(loto[i]+" ");
}
System.out.println();
}
}
}
留言列表