import java.util.*;
class Alex1004_1
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入測試的數量:");
int num=sc.nextInt();
int stat[]=new int[10];
for(int i=0; i<num; i++)
stat[(int)(Math.random()*10)]++;
System.out.println("編號\t數量\t比率");
for(int i=0; i<stat.length; i++)
System.out.println(i+"\t"+stat[i]+"\t"+(float)stat[i]/num*100+"%");
}
}
執行程式結果:
請輸入測試的數量:10000
編號 數量 比率
0 1058 10.58%
1 1004 10.04%
2 979 9.79%
3 993 9.93%
4 998 9.98%
5 1014 10.14%
6 963 9.63%
7 1026 10.26%
8 1025 10.25%
9 948 9.48%
import java.util.*;
class Alex1004_2 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("請輸入抽牌數");
int num=sc.nextInt();
String[] suits{"♣","♦","♥","♠"};
String[] result=new String[num];
int cnt=0, A, B;
String card;
while( cnt<num ){
A=(int)(Math.random()*13)+1;
B=(int)(Math.random()*4);
card = suits[B] + Integer.toString(A);
if( ! Arrays.asList(result).contains(card) ){
result[cnt] = card;
cnt++;
}
}
Arrays.sort(result);
for(int i=0; i<result.length; i++)
System.out.println( result[i] + "\t" );
}
}
程式執行結果:
請輸入抽牌張數:5
♣11 ♦8 ♥1 ♥2 ♣11 ♦3