返回列表 發帖
import java.io.* ; // input output 資料函式引入# J$ d+ J: o3 K4 T
( L! C6 _7 F3 j1 g+ Y% _0 ~% Y
import java.util.Arrays ;
& ^! `3 q& l- s, X  S% N. n/ L6 _7 P' y! i+ w1 q

7 B+ D+ F2 G* `+ Z+ W4 |% [
+ M, w' ^+ s8 ~public class JVD103{. J" `& x/ u1 K
( ]: O7 ~% H' f  r

8 r. J( W' ^+ N) m* \+ l3 H0 ]5 c2 a; J
        public static void main(String arg[]){  |8 C& f' a! z
7 j+ X* h& _' L
        
/ i) y- u5 F2 O. `& w3 I! }) ~! J* R; {7 N, w' M' f  t2 g
        
3 D7 I: ?" _& W4 M' T1 \. T/ w+ @
                System.out.println("請輸入欲產生之亂數個數:");9 R" M3 s" w! N- r7 ?+ v
4 J$ y! r, v- m% `; h4 `) _
        8 |% r5 F3 V& t* [+ {! J

5 S2 D2 p& Y( i4 n6 O                int n = 0;  //輸入要產生亂數個數 並初始化
3 Z# T' m8 u, ]3 B: r8 l: e' Q9 ^7 h' G" I* x7 O! [! O! g
               
' _- R2 J9 ~( k2 S$ n1 W8 |
' Z. S# s: `0 u9 s0 y1 P) D                //使用到io 一定要加上 try catch$ ]6 {7 n6 l9 ]5 X
6 I; c5 l. v* _
                try{
  j) V/ f+ g2 \8 V; e1 w; y0 P3 k3 p" t: q# v3 a( a
                        InputStreamReader isr = new InputStreamReader(System.in);  //輸入資料流2 m: k+ q7 l4 b! P* w8 l% {
: k- v+ l( C# o
                        BufferedReader br = new BufferedReader(isr); //資料暫存區: F. h# Q  G4 ?
% g, P, L5 E8 a) c* W' \( N) d, |; _
                        n = Integer.parseInt(br.readLine()); //輸入的資料是String  但我們要的n是int (轉換)6 j' H  G' R. P1 R7 d

  [4 E( ^2 f: d0 x% A9 K) Q9 D( u                }catch(Exception e){}$ X! \& z# n, m( P% B  A, K1 W
% E8 P: b, s( ?7 r: J) @5 O+ M
                0 J; y7 y! ^! p! c6 }

8 J9 C/ s' `. G6 F, _7 F                , j: t9 j2 T4 n2 Z

* g# q% s5 ?8 f' b1 y/ ~                int Num[] = new int[n];//產生n個亂數的變數
& h) R, G, Q! t- F# N" {
6 Y. \; K" t3 b. G" Z               
% P/ ?" p$ T6 \
5 R; d9 O6 V& |# V' c/ \                for(int i=0;i<n;i++){; `1 s1 }( |+ A0 |

& [' Q. h6 f5 ^                        Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)7 f" e6 q: F# g* g- e

/ {& }6 @. K5 j8 I$ X7 Y8 Z                        //System.out.println(Num[i]);5 X6 r5 @9 L8 \& {( S
7 q- ?+ w3 N- g2 W% `
                }
3 G/ `7 f/ ?4 O) W4 w/ Y; P  G& O
                Arrays.sort(Num); //排序" w' w& |; w8 p' H! w' ^

, a: F3 ?2 q5 q0 r                5 m6 a$ z/ \6 X! I" w" H7 C
. m( s3 W6 q: a! p& y1 |, G
                //輸出
0 ~4 R( G7 o& x$ j$ ?/ z, U# H; E/ S0 y
                for(int i=0;i<n;i++){
' l+ P0 X5 v  }
1 q5 c& W7 K3 `1 I* F* s/ O                   System.out.print(Num[i]+"\t");
& ]/ \0 b# v! y  A: G1 `# K/ m- X: @" c# `3 B& g
                }
1 f5 d3 o3 T; y) L4 w/ T" ?9 W8 m0 g2 Z4 ?4 `% G
               
+ j8 x, Z! ?# g' Q$ @. s; j1 Z. s7 q6 V; [* b" N
        }
6 Z2 G& M- i8 P# d+ X% I* P. ?
+ [8 q$ a) J. _) z# q% M. E2 l) h9 C

2 H; P  ]) E) p8 k9 F5 G}
人平

TOP

  1. import java.io.*; //input output 資料涵式引入

  2. import java.util.Arrays;

  3. public class JVD103{

  4.     public static void main(String arg[]){

  5.         System.out.println("請輸入欲產生之亂數個數");

  6.             int n=0;

  7.                 try{

  8.                 InputStreamReader isr=new InputStreamReader(System.in);  //輸入資料流

  9.                 BufferedReader br=new BufferedReader(isr);  //資料暫存區

  10.                 n=Integer.parseInt(br.readLine ());

  11.                 }catch(Exception e){}

  12.                 int Num[]=new int[n];

  13.                 for(int i=0;i<n;i++){

  14.                     Num[i]=(int)(Math.random()*1000);

  15.                         System.out.println(Num[i]);

  16.                 }

  17.                

  18.                 Arrays.sort(Num);

  19.                 for(int i=0;i<n;i++){

  20.                         System.out.print(Num[i]+"\t");

  21.                 }

  22.         }

  23. }
複製代碼
人平

TOP

import java.util.Arrays;
$ T7 |. [( q) h+ @" \2 u2 A
/ |& j, G; ?) m9 Z7 X: pimport java.util.Scanner ;9 [( g. n8 W$ z4 R  V

. _  C4 T4 c* c. |public class j103
9 I0 Y7 |& r' S
! o7 {- b; q; E{
. x! w+ r, V6 v1 w2 J* }, t# W& B" z  r! m% U/ y
        public static void main(String[]arg)  v3 E, v! B* I3 v6 H6 p
& ]3 C( H- ]3 D5 D+ v; Y. T6 d' O5 s
        {- L6 V0 E& R0 u# e9 N2 X
" _# W; V, Z: ^! s
        System.out.println("請輸入欲產生之亂數個數");
; `  n" N3 e  n1 q# a3 ]# K  H& @. N8 _; l; B
        Scanner s=new Scanner(System.in);- o' j/ T" e& R% ^1 t+ n
- d' A: w+ [# v
        //System.out.println(s.nextInt());- |, F5 ~- C3 t6 D
, M5 ~3 p8 b7 a' b7 L
        int n = s.nextInt();5 W9 }5 n" X- m$ I! O' \

; P" I- H5 S! n6 V( x. b        int ar[]=new int[n];
% ?/ j+ X' d3 T. ~4 z, b2 B
. q3 k, O" i, ~/ x; c: @                for(int i=0;i<n;i++)
0 w+ i1 L: u$ x2 D! t  R0 x6 r% e5 E' H& s8 q$ T
                {
/ d( J8 i+ E& O3 Q1 \1 b9 n/ c1 z) |4 e1 ]/ t8 Q
                ar[i]=(int)(Math.random()*1000);
$ S. V9 j* s2 j9 \! e
; {+ {4 F$ M' o. V3 y/ A! @                }
; |( v( \* l; R& \9 \. T2 V& G  k1 p5 P6 g" a2 h
        Arrays.sort(ar);
  N1 j. R4 [0 D! Z$ C! R
# j7 }4 i4 c2 R3 ?& y, H; O* A2 h                for(int i=0;i<n;i++)
! ]: `# b- @; K, f: c3 I1 l- W& ?5 {: v( I( k
                {
$ S- A1 |$ [( [4 x0 ~1 U) \0 Q; p6 G: v# E# x
                System.out.print(ar[i]+"\n")        % u$ [4 s' T1 Z0 ^  N6 [; S
# Q* P4 S* i0 b# m9 }9 }# E: n
                $ y( X1 y! D5 P6 D- p
$ j0 `, g7 D5 z( ~) U
                }5 y" K4 a. B. i( E0 V  u0 t" [$ @/ q9 o% M
$ r8 \- I- O& {
        }9 y9 {; y! b% k5 q
- I. F/ W% [( `6 z7 }" O# |
}
人平

TOP

返回列表