import java.io.* ; // input output 資料函式引入, y. Y( z* g% f# n. ?* o1 C1 q
1 r2 C% b$ k2 {$ g9 |0 W& Pimport java.util.Arrays ;
- Z G/ L2 h% {: U5 A; g# p# R* s
& D# u# J( P7 _# y P. u
9 I3 L3 G9 {1 L. c/ A* R1 lpublic class JVD103{" X$ O+ A: J5 Y9 T6 [
3 ^7 e/ S3 _8 n3 y/ y5 K x2 A
& S1 A% D6 c0 |5 v
& X( A+ Z% v( i" o5 g7 e
public static void main(String arg[]){. E6 `- |$ V& y' ?/ }- A
5 d3 E0 h2 ]& r! x
+ S7 o$ g5 h( t! y! @" ?" f
# I6 w, M% O; B
* ?" G- H- b" G; n `( ~' H- q, s! g8 [/ a, _2 S1 b
System.out.println("請輸入欲產生之亂數個數:");2 G4 E9 y8 j% c
+ C7 P1 L5 P" b! M/ g/ p
* X' J h& X# k& Z/ i; G1 F0 b* X
int n = 0; //輸入要產生亂數個數 並初始化
. r: G& k" X: M+ t1 B
( i0 R u9 o# d: U6 F, ?1 ? ! A! ], w% l( c2 I
* q: z7 Y' k8 q
//使用到io 一定要加上 try catch0 C. h+ R; X( Y4 \$ P! f
2 \+ `2 _: F- o" t/ i0 z3 K
try{' O" @8 E7 f$ ` R. t
) R* ~: ~' Z# ^+ ]$ N# c
InputStreamReader isr = new InputStreamReader(System.in); //輸入資料流
% z9 L' N# Q7 s. r
/ }6 P2 q% I; A/ k( J BufferedReader br = new BufferedReader(isr); //資料暫存區' f: ~; I" n2 X: y( f3 e
# s+ w+ s4 U8 ?3 X) M) }$ V+ C4 X; n2 p3 k n = Integer.parseInt(br.readLine()); //輸入的資料是String 但我們要的n是int (轉換); l T1 [) q) Y7 u
6 ]: {( R; I4 I
}catch(Exception e){}, w, b5 B2 _. ?) p) q3 ^ F+ v
3 B' X2 x( k' |5 E1 T. \' c" g& F" \
. \% k+ Y K7 x4 b! }& Y- d
: h4 S/ g5 D2 |& t4 X, x
' e) |* B0 b% ?$ r
" ^: C8 K5 c3 R, j, N1 V int Num[] = new int[n];//產生n個亂數的變數3 F9 L( Q" n) Z6 m
) X+ _. j; G7 d$ w $ ]7 n3 s/ Q* t/ C- N9 M
- h" I& B) _ C# n5 S! k. A6 R) e4 f l4 e for(int i=0;i<n;i++){- Q k, u: e# x, F8 X
N, A0 w0 ]# S8 S) z9 W Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)
: m) G+ ]* K& P" L5 |
# u# v/ A( u! _ } ?6 z* M; u //System.out.println(Num[i]);; u, Y* I `6 b1 v
! }3 v+ ~& P/ b1 Y* {" B0 U/ p6 Z
}
& B# I8 k1 d* h. v
! ]3 G7 v$ G* c8 N% W ` Arrays.sort(Num); //排序4 J1 L, d+ [7 _& o6 Y
9 }8 V0 w% l0 K 3 F) v0 K+ v' w' W: m) R; h, A
* n0 t- P' p W9 B4 q F //輸出
, j! r, k# p+ v; B$ Y" u( }
2 T# o6 x0 K2 J$ q for(int i=0;i<n;i++){
. m9 F: q4 Z L- p# E$ u f) k
( A/ U# h% D/ M9 V System.out.print(Num[i]+"\t");) ?" r* r* z1 k* P$ t/ G5 d
: c8 {/ |. f/ L0 I+ B
}5 c4 H% ]! u1 }: y, f
% l; \, z; s; p* R* Y# n
, v% V9 P2 I3 b- p+ w
4 t z5 L( C& Z, ?) [
}- V; [0 t8 E) d: _
- y- ^2 j% Q% X4 t! C/ X0 X5 z" X
9 W& R7 V: x3 m8 ]1 z0 V* l. s4 r: w% @7 [7 ?* T; i9 N3 U* s* f
} |