2018年12月22日 星期六

[Java]_ch8.7_大樂透物件導向設計1





[Java]_ch8.7_大樂透物件導向設計1

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Arrays;



public class Loto {

int A[]=new int[6];

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader buf=new BufferedReader(isr);

int num;

public Loto(int n) {

num=n;

}

public int UserInput() throws IOException {

System.out.print("請輸入隨機組數: ");

String s=buf.readLine();

int n=Integer.parseInt(s);

return n;

}

public void randomLoto(int n){

for(int i=1;i<=n;i++) {

System.out.print("第"+i+"組:\t");

A[0]=(int)(Math.random()*num+1);

do {

A[1]=(int)(Math.random()*num+1);

}while(A[1]==A[0]);

do {

A[2]=(int)(Math.random()*num+1);

}while(A[2]==A[1] || A[2]==A[0]);

do {

A[3]=(int)(Math.random()*num+1);

}while(A[3]==A[2] || A[3]==A[1] || A[3]==A[0]);

do {

A[4]=(int)(Math.random()*num+1);

}while(A[4]==A[3] || A[4]==A[2] || A[4]==A[1] || A[4]==A[0]);

do {

A[5]=(int)(Math.random()*num+1);

}while(A[5]==A[4] || A[5]==A[3] || A[5]==A[2] || A[5]==A[1] || A[5]==A[0]);



for(int j=0;j<A.length;j++) {

System.out.print(A[j]+"\t");

}

System.out.print("\n排序後:\t");

Arrays.sort(A);

for(int j=0;j<A.length;j++) {

System.out.print(A[j]+"\t");

}

System.out.println();

}

}

}


沒有留言:

張貼留言

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...