import java.io.* ; // input output 資料函式引入8 t# r) d8 P" V: [. T
4 h( X( ]( Y- m+ @/ g% c
import java.util.Arrays ;
0 A Z5 u3 L( y, G; P: T c
+ y/ v' {$ X$ X7 ?9 N' b7 C8 N9 G5 R7 @; a, r9 N8 A' h6 |
' o! H0 J2 [& }) b4 f, @9 C( P
public class JVD103{- f, M4 \ }- l$ k
# b7 t% y, X0 [6 Y& y9 l! [/ Q
# d% i E! n- H I
public static void main(String arg[]){, w0 L1 b' O" ~9 H# [0 B1 {$ P
5 M. i |8 w1 T: A9 F
, j! s8 {* O+ ?# P( z7 u
9 Y0 t+ x y! j Q- l
! I. t# z! Q5 F1 B( {2 j# z$ g
* C9 p& n2 D% {" j6 ^ System.out.println("請輸入欲產生之亂數個數:");
" F2 Y( A) @4 ~% n
4 e+ |( t, H4 q6 ?6 z9 y o- J 6 q. t* D; k. R: t9 E: _) \
4 `* Z: R1 x+ ?) ?2 s$ L1 W, c
int n = 0; //輸入要產生亂數個數 並初始化
: t; c) a! }7 ?9 h$ ~% }2 q6 [3 x, Z3 E7 \" U% l) a
' P3 x: n! @7 u4 S
# L z1 b% w5 ]: R E0 {3 S //使用到io 一定要加上 try catch
2 \5 [( v; Z& S$ r- \% I% Y: I0 p" P2 p0 ^+ g
try{4 j) ~7 C# J) ~2 g! d
" \5 c& m" [+ V) C
InputStreamReader isr = new InputStreamReader(System.in); //輸入資料流5 p& j0 _1 [/ R5 B
8 t' p% `1 B8 i# g3 F: i. k
BufferedReader br = new BufferedReader(isr); //資料暫存區
V/ @) |0 R! c, \+ J+ D- h8 b) B5 w
n = Integer.parseInt(br.readLine()); //輸入的資料是String 但我們要的n是int (轉換)
6 X7 x( N$ T% M0 f$ I1 b. F
* ^% {0 a- M# g, k }catch(Exception e){}0 M% m4 E8 Q6 G, W% V0 Z% h
; K6 m- _- }! Q; Z( T' V
1 c% k: k8 v" p$ z& p+ ]& Y3 i8 ]% O1 T0 E# _% u
$ X" y D, x) `
. r, |# J) j$ [- A/ r
int Num[] = new int[n];//產生n個亂數的變數1 ~1 a" e- l, C: ~; C% s
' B* C2 s& ?' Y ) R* P- _, x0 F
# G- Z; j1 V g; Z2 H4 v! P
for(int i=0;i<n;i++){* C( b) f- U9 a u
" L% k/ E% p1 l! ]: S0 P Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)- M7 e9 Z% b! _+ A
4 k n/ p) G2 O6 V
//System.out.println(Num[i]);) E! W$ H* [: ?& A& f' b: j8 z' i: L# l
* y% m2 f" I8 c, d3 J( t3 T6 k }
. T- S% f9 E6 s- [; w% L1 ?1 s, Q2 ]" s
Arrays.sort(Num); //排序
k9 U/ e7 N) h5 T2 B* ]4 [& C; Z& v! G
6 r6 s+ |$ p+ o# S. {) y
A% I6 t8 v- S+ x" L //輸出; V6 L" m# f {! z0 r7 j
; x& f- e8 d0 r: U for(int i=0;i<n;i++){
q) p1 O R+ e0 Y6 v7 r1 _7 C. w4 q! K T( f
System.out.print(Num[i]+"\t");) f! n7 u. c Y6 h. n
% Y1 j. R/ K P7 F
}
0 q2 J* L- s; v' K8 `8 n( @: K
3 V7 ?. D5 D. U7 _7 q
! b/ o% U+ H1 A1 O' {8 t* n& k; t+ _% c& V" R G: d/ D
}4 }' P5 z! B4 i4 \4 z
' `# Y, g. O N
* K$ c/ a# Q% U# A. H _0 L, ]; @" Z1 K" L! D
} |