import java.io.* ; // input output 資料函式引入
, @8 ]* G$ n* O$ I: i/ F! n. q
4 C) @) W! \. l# i0 gimport java.util.Arrays ;% j, E& [# \! ~
# x( M+ L/ o C }6 f; w& o5 Y, }* A$ H5 J$ S* Z
: P4 {4 V1 T& Q
public class JVD103{# D- \5 [5 d3 `4 A: u* U( X4 Q/ q# x" K
$ ?( j: u; I1 \. @' }* [% y: s& j2 q. f
/ _8 W/ X4 H y7 h# s3 t6 C
public static void main(String arg[]){
6 r3 x9 }! \% s* T
0 D1 D3 f4 g. [3 T: K% p' Q6 @ m/ W+ g+ ^. p) T8 m Y
# [; N, {/ T" \* Z) W* M* a
$ A0 R- m6 T6 m V3 K' ]% t
4 E+ g" `/ ~* f. A/ ? w System.out.println("請輸入欲產生之亂數個數:");, m" W+ G* ^0 M/ m) e' _5 a6 z/ G
; p6 g5 |( c# f! _( n2 e {# @ 5 i; [$ P1 S# I% [) V& P& f
3 x* m! g5 U9 ~. X
int n = 0; //輸入要產生亂數個數 並初始化
/ v+ p. v" C8 e* [- V3 `% A5 P6 \" c3 E
; v8 { }4 Q; G4 G7 v5 U5 j- p
9 q! J8 J- Y6 \ //使用到io 一定要加上 try catch
! O/ F# M2 R) \' r% J
& u5 I2 j+ q( I5 k* B try{+ Q- i% x( z( p- d* J+ l; L
) [8 z: |! V9 y+ j
InputStreamReader isr = new InputStreamReader(System.in); //輸入資料流
0 v0 [3 k! n" }1 [. w. I; o9 t1 O0 H4 U4 }/ e8 E& N
BufferedReader br = new BufferedReader(isr); //資料暫存區: ^& `, m7 z' J) {
& g$ T" Y% L D% O5 c0 F+ I n = Integer.parseInt(br.readLine()); //輸入的資料是String 但我們要的n是int (轉換)
4 p: a# N, o) c3 U7 l A0 b. j- k9 P2 Y" }, I4 A
}catch(Exception e){}; d) s0 L0 Z9 I6 j9 p5 @+ ^
8 h6 R# L+ l1 ^/ D2 I; [
! B. e. ]8 p5 |7 S! |/ e( o5 s g1 b. q" R. o$ Y
+ _4 U3 o9 x' g" S, T4 M. K' o
* d4 M1 H" @1 N& A$ R int Num[] = new int[n];//產生n個亂數的變數
8 R3 L! h$ X6 {& ?; S+ S' t+ l( q2 Q U* t# i5 b/ a
" T0 y5 f! J1 i, v, D
; w! l" R( f" E, _; v for(int i=0;i<n;i++){$ \" s2 a1 E" g; f& M0 T" k! k
- B) x. c v+ V Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)
$ t0 I. b' y) H! l, l$ w6 q j# q( h: w
//System.out.println(Num[i]);
8 m, p' u9 N9 U7 v2 g6 w2 q' ?/ H7 c$ O3 Y# ]6 ?
}. a& M+ c6 G# ?- i; P( Q
7 o5 Y, e5 R$ }5 e6 B+ e' d Arrays.sort(Num); //排序
$ D. n& i. b d# g3 Z b8 j1 v
' m" b4 n3 c% N/ w* ?$ U) c
: n) |7 Y7 N6 F1 c( W+ x% R
. Y7 f+ T! C% x //輸出
+ W t/ S* i L" s0 k$ Y: i3 X1 G# m9 l. q* M+ k
for(int i=0;i<n;i++){
/ {: L, Y9 x8 j1 P
* B4 I9 m7 i! S System.out.print(Num[i]+"\t");
, r2 R( I$ `) l- n, Y; l, J6 ~ t" M# f% M( h0 U7 L
}; [0 C2 Z0 C5 T0 H+ j [8 @
' V$ z }$ S* i) T1 `2 ?2 C/ x
- c+ m4 P) ?/ O: Z% b" \2 i+ [7 b$ e
2 ?' o2 t$ A" a5 I }
2 O) h5 E; ]# I" \+ Q6 d4 o5 l, p
9 y$ e! m; \" B6 i. y5 ]8 f8 ?, R, R- u5 u0 L
% U/ A) B2 g( ?6 `; b% E5 U} |