import java.io.* ; // input output 資料函式引入
) d( V! w1 r `% I7 J
( W" z; T b5 M" T+ vimport java.util.Arrays ;
- F6 t) i8 [/ Z
& m$ h$ q3 c) D$ c' S
* G0 {8 ?. p' V
9 y" N; _/ ^0 Q) }( w W! t: a- A3 u4 ~public class JVD103{
0 k8 P0 ^( j+ ^+ C& L) F6 n5 ]
! \/ r; I: K( q O: N, X3 n# d% u9 M' o- ^; _' s5 z4 S
! m; O$ n# y: k public static void main(String arg[]){3 t6 D$ J+ ]; g7 T4 }" v8 h
$ P0 G2 Z7 @7 r; l5 o) x+ k
+ z! A, ^$ d) I Z$ s% p, W7 N0 i4 B0 k; y- E
- h. i2 R4 e. U8 ]# L: M. l: d) g, q: C* I
System.out.println("請輸入欲產生之亂數個數:");4 @! T. F H' Y: A
; ~% F: j+ y8 y3 l5 q/ ^1 ~ # _4 T& p( q2 q! y
. F8 [* C/ v" s
int n = 0; //輸入要產生亂數個數 並初始化7 {5 [8 ^+ J7 E! S
7 u; w% w7 [6 ]4 w2 [
/ W- q& }4 E" i$ M! U( b& C( D5 \9 t- K' c0 o
//使用到io 一定要加上 try catch
" k0 s6 h. B) o4 U# L; I. u% s, y& T
: I' T+ ^. E8 P. u3 U( C' e try{
- i. `% p& { a5 y* L, Q* ]5 _) T: W. D( p8 J! P
InputStreamReader isr = new InputStreamReader(System.in); //輸入資料流
2 G( O1 U0 }3 I& O) f" S U- v- h9 b! {4 v4 g2 w
BufferedReader br = new BufferedReader(isr); //資料暫存區
- O. U; r; T9 D0 ^; L
+ a& B, R; i/ o/ s) R1 w n = Integer.parseInt(br.readLine()); //輸入的資料是String 但我們要的n是int (轉換)
8 Q; V9 q2 I' z# e
( X% @) F8 C& R/ p0 q" @ }catch(Exception e){}
# k8 ^5 _3 A, j" i& ?# y: N: b- I+ x* N1 \2 g
( g4 m# Z) O$ _. \- A( A
" f0 [2 j7 x4 E
9 V/ I1 E2 B, N' m7 w- K9 D
4 ^* Y; G! x9 q4 z# z$ s9 ^
int Num[] = new int[n];//產生n個亂數的變數# X, Q K/ [, s, W( O- X* Y
& k4 C8 Z+ d. `
. Y( {# u! x* F6 [2 D$ O; T2 l- ^% [% g7 e/ f: d' Q/ A5 j/ Z
for(int i=0;i<n;i++){0 [# E; X7 [6 M; C' c9 r" E
3 N. D& t+ \. z7 C* k3 a8 H5 E) G
Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)
- i5 o* f- `" H) `
4 s) m& N) W4 R0 M //System.out.println(Num[i]);
! u" U" I: b( X2 U4 d1 h: [1 i: K. u- Q% z
}
3 R' e% U$ u- Q& f) Z) Q2 O
1 F7 t3 |( J/ V Arrays.sort(Num); //排序
" V$ e' X6 ^% q t, h! a
4 [. i W/ Q4 ]& E( `- s$ ]$ A % [ s, a8 z2 _( N; w* [
- U% @3 }; D0 g% F
//輸出
! H) D- D* m5 O0 x' X) N0 p9 w0 S6 w
" ?8 n# `# K- L' [$ u2 l$ p/ V4 V for(int i=0;i<n;i++){# k' L* e! P7 C$ {7 n) R
" n1 G# ?' c- }# \9 V System.out.print(Num[i]+"\t");8 b$ T/ K7 o: i- l% ^' e
" b0 F1 F3 j8 L, V X4 s
}
# M3 \5 T% q2 B; S. @( ]
# U# I% l9 g. Z ( ~4 {3 p$ A1 \4 c
4 }; E* r6 s: O, l# v) c+ p
}5 `/ U1 {" o W R$ \
! X3 s z' w; I, h; c2 v/ K6 \* e! X. u
1 p' Y" O. x/ k3 B g
} |