返回列表 發帖

1217 使用者輸入值(實作jvd103)

本帖最後由 b790113g 於 2011-12-31 09:54 編輯
5 b4 C7 W% A0 ^# \& z/ J. `1 B/ f: t, j/ J0 z0 v; O% k
程式中可以輸入值- ^) |6 P2 f) P1 ~: ^" o, H
% ?( x9 K$ h' y# y3 Q0 \% l, ^. J
實際寫出 jvd103
/ m- u4 Y- K  @! E
2 h6 T  I/ d5 m% a& E使用io一定要使用 try catch
  1. import java.io.* ; // input output 資料函式引入
  2. import java.util.Arrays ;

  3. public class JVD103{

  4.         public static void main(String arg[]){
  5.        
  6.        
  7.                 System.out.println("請輸入欲產生之亂數個數:");
  8.        
  9.                 int n = 0;  //輸入要產生亂數個數 並初始化
  10.                
  11.                 //使用到io 一定要加上 try catch
  12.                 try{
  13.                         InputStreamReader isr = new InputStreamReader(System.in);  //輸入資料流
  14.                         BufferedReader br = new BufferedReader(isr); //資料暫存區
  15.                         n = Integer.parseInt(br.readLine()); //輸入的資料是String  但我們要的n是int (轉換)
  16.                 }catch(Exception e){}
  17.                
  18.                
  19.                 int Num[] = new int[n];//產生n個亂數的變數
  20.                
  21.                 for(int i=0;i<n;i++){
  22.                         Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)
  23.                         //System.out.println(Num[i]);
  24.                 }
  25.                 Arrays.sort(Num); //排序
  26.                
  27.                 //輸出
  28.                 for(int i=0;i<n;i++){
  29.                    System.out.print(Num[i]+"\t");
  30.                 }
  31.                
  32.         }

  33. }
複製代碼
  1. import java.util.Scanner; //輸出入
  2. import java.util.Arrays;  //陣列

  3. public class jva103{

  4.         public static void main(String args[]){
  5.        
  6.                 System.out.println("請輸入欲產生之亂數個數:");

  7.                 Scanner s = new Scanner(System.in);

  8.                 //System.out.println(s.nextInt());
  9.                
  10.                 int n = s.nextInt();
  11.                 int ar[] = new int[n];
  12.                
  13.                 //產生亂數
  14.                 for(int i=0;i<n;i++){
  15.                         ar[i] = (int)(Math.random()*1000) ;
  16.                         //System.out.println(ar[i]);
  17.                 }
  18.                 Arrays.sort(ar);
  19.                
  20.                 for(int i=0;i<n;i++){
  21.                         System.out.print(ar[i]+"\t");
  22.                 }
  23.        
  24.         }

  25. }
複製代碼

import java.io.* ; // input output 資料函式引入
% I) T6 K) _% @4 i7 N* ?2 M% n$ G. {" W) [( o6 ]
import java.util.Arrays ;
- Q. d0 V8 R' X& l+ Q
  S/ q: R" K3 G2 J4 c
: W- p6 H4 [6 T* w% ^) E1 M% }4 j6 d3 F3 G8 h
public class JVD103{
: n8 U9 V# f1 U- x. Y5 Y- Z0 ]( K: x! `3 u6 c' I, B- q1 I

5 q! J" v6 N* w! N. }9 ^1 [% Z4 `# b0 `5 L
        public static void main(String arg[]){
/ Q1 P0 n# g; l1 I8 T
" z3 v# V6 C1 n& s+ a, R' A        : a$ f6 r. h0 u! T# i& k
! e. Y  _* L/ A# E
        1 @# G# j! R3 P) M" l5 I: ?9 z

4 N5 T/ H) h" P! X5 r                System.out.println("請輸入欲產生之亂數個數:");
, L# E, p% }/ ]) S7 x
2 O8 e( f8 p+ @' E        
, m) K5 r: o; I  S4 @& M# ]5 c: [8 g' z
                int n = 0;  //輸入要產生亂數個數 並初始化; V/ A5 t' j* Q+ t" a5 I2 \
7 M4 ]2 J5 w. e
                . Q( B% s# P6 s7 Z7 `8 T

5 Q$ x, [* g4 N6 A2 |3 t                //使用到io 一定要加上 try catch, o: s* i* b, |9 y2 M

  F2 M/ |7 K' F# R                try{% B2 T* `. x- u
* {1 k% [9 ]8 ~' {% O) e1 n" C
                        InputStreamReader isr = new InputStreamReader(System.in);  //輸入資料流9 s0 L& a5 T, t1 G

  T2 }9 ]5 R6 N: X                        BufferedReader br = new BufferedReader(isr); //資料暫存區
) H7 \; B/ d( I0 N+ a/ D
' S2 H5 r2 B$ u; B' o                        n = Integer.parseInt(br.readLine()); //輸入的資料是String  但我們要的n是int (轉換)1 W+ Y, k' R, L6 C' M$ [0 |5 D( B6 d

* k1 a; D2 \- T% L" B/ N# Z" Z                }catch(Exception e){}' I) k8 Z  Z# t8 @1 f

- v" s. [# L, T" S$ z               
7 }7 A9 d; U% z7 y% v) u0 c+ x/ p. W( t" m
                / p  b- G$ l7 r- d
6 _4 R" R! i, t# m9 |$ Z: x+ T' Q
                int Num[] = new int[n];//產生n個亂數的變數1 b9 L) P1 j2 s- ?0 A

, |6 V4 X) j. {               
& h3 E0 h* T7 D; H: x$ m0 Y2 f# N* x) E0 z  r+ E, j
                for(int i=0;i<n;i++){
+ e( W/ z& O7 q# I4 t# S7 Y0 C& P
/ w6 p' Y8 _- S3 D                        Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)
7 n: ^$ J; N1 F: P; b, w3 Y+ C4 _$ U1 D( \% Q+ F% D  ^
                        //System.out.println(Num[i]);' ?+ s$ M! o! [! k
  v8 y/ `+ _- r- M" T
                }1 ?4 }! L3 m9 y6 G* S2 X: m7 l

% |- l9 {# ?+ T                Arrays.sort(Num); //排序
% T1 V  `$ i. \6 @( m$ N* U0 r
. [* L3 X  V1 f: |* Y               
8 m' U- A0 u5 @1 |
  Q/ x7 N8 f0 o% n( X# R                //輸出
) c- C8 c5 G3 q5 H7 L+ A/ P. ?$ J, K1 F3 H9 ]9 r, N
                for(int i=0;i<n;i++){
9 \* c' Y. B5 i& t4 R* m' ]& L
3 }# o! k( i& }4 K; p- `7 h" J+ s/ B                   System.out.print(Num[i]+"\t");
5 H  a: F9 \' }' K4 z6 k7 H: Z3 K8 J( y6 Q4 D: Y
                }
+ x2 Z. h0 Z0 x; a  m( y2 Y, A, ^- ?" r2 y
               
7 E# p. u2 q/ D, d2 }: P
7 p" E! v' e& j9 l" _% d        }. }7 J& n4 {% P% R/ H

$ M/ ]3 G5 N) j. j  w) ?+ }. t7 ?0 w8 d

; t; T, l6 [, [  w% Z; g3 t}
人平

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

  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. public class j103
  4. {
  5.     public static void main(String[]args)
  6.         {
  7.             System.out.println("請輸入欲產生之亂數個數:");
  8.                 Scanner s=new Scanner(System.in);
  9.                 int n=s.nextInt();
  10.                 int ar[]=new int[n];
  11.                 for(int i=0;i<n;i++)
  12.                 {
  13.                     ar[i]=(int)(Math.random()*1000);
  14.             }
  15.                 Arrays.sort(ar);
  16.                 for(int i=0;i<n;i++)
  17.                 {
  18.                     System.out.println(ar[i]+"\t");
  19.             }
  20.         }
  21. }
複製代碼
小雲雀

TOP

  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. public class j103
  4. {
  5.         public static void main(String arg[])
  6.         {
  7.                 System.out.println("請輸入欲產生之亂數個數:");
  8.                 Scanner s=new Scanner(System.in);
  9.                 // System.out.println(s.nextInt());
  10.                 int n=s.nextInt();
  11.                 int ar[]=new int[n];
  12.                 for(int i=0;i<n;i++)
  13.                 {
  14.                         ar[i]=(int)(Math.random()*1000);
  15.                         // System.out.println(ar[i]);
  16.                 }
  17.                 Arrays.sort(ar);
  18.                 for(int i=0;i<n;i++)
  19.                 {
  20.                         System.out.println(ar[i]+"\t");
  21.                
  22.                
  23.                 }
  24.         }
  25. }
複製代碼
★ 嘉凱~~☆

TOP

  1. import java.util.Arrays;
  2. import java.util.Scanner ;
  3. public class j103
  4. {
  5.         public static void main(String[]arg)
  6.         {
  7.         System.out.println("請輸入欲產生之亂數個數");
  8.         Scanner s=new Scanner(System.in);
  9.         //System.out.println(s.nextInt());
  10.         int n = s.nextInt();
  11.         int ar[]=new int[n];
  12.                 for(int i=0;i<n;i++)
  13.                 {
  14.                 ar[i]=(int)(Math.random()*1000);
  15.                 }
  16.         Arrays.sort(ar);
  17.                 for(int i=0;i<n;i++)
  18.                 {
  19.                 System.out.print(ar[i]+"\n")       
  20.                
  21.                 }
  22.         }
  23. }
複製代碼
水桶小鄭,鯰魚

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

  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. public class jva103{
  4.     public static void main(String arg[]){
  5.         System.out.println("請輸入欲產生之亂數個數:");
  6.     Scanner s = new Scanner(System.in);   

  7.         int n = s.nextInt();
  8.     int ar[] = new int[n];
  9.     for(int i=0;i<n;i++){
  10.             ar[i]=(int)(Math.random()*1000);
  11.             //system.out.println(ar(i);
  12.                 }       
  13.         Arrays.sort(ar);
  14.                 for(int i=0;i<n;i++){
  15.                     System.out.print(ar[i]+"\t");               
  16.                 }
  17.     }
  18. }
複製代碼
May

TOP

import java.util.Arrays;) Z9 o: o; @6 e# R2 ^
9 z8 F. R: }4 G- ?# L
import java.util.Scanner ;
5 X8 f; M' K7 q0 y( K
0 Z5 f  m( X" i- p" K$ P2 Zpublic class j103
$ i6 {  q0 J6 K- K0 g3 h' U2 r5 P; b# a+ m5 P! K
{
7 F' N7 L2 M; r. D! W3 \% |8 A: u! c2 F
        public static void main(String[]arg)
8 e4 T7 U1 l2 P- S
. a: F+ {5 E8 g8 U9 V        {( t, s4 t. d. d  t3 A4 l$ w
6 i" \, V: P, J1 r
        System.out.println("請輸入欲產生之亂數個數");6 r1 z8 \0 t" q

! X) a* Y! j( c; R! X8 f        Scanner s=new Scanner(System.in);
9 R# i3 q9 p0 h. h3 ~! V! ]/ T, {: J
        //System.out.println(s.nextInt());
; i- n$ _2 j, A* W
! ]& }) c+ U& m& O        int n = s.nextInt();7 Q# ^4 J' D/ }8 r$ O8 ~; ^4 L
2 k" }2 g6 q" r( a, {
        int ar[]=new int[n];
: q& v3 m" \' m' _- W9 d
* s0 Y0 q$ [7 r4 q! x                for(int i=0;i<n;i++)
$ g5 G3 L* ^% k
& e" m. |# A7 x                {
# S3 N! f" h5 H0 G. {% }% U+ o, F9 p6 ]: z8 ~
                ar[i]=(int)(Math.random()*1000);9 o( _+ j: x3 i6 b

& P# r6 a/ `2 U7 E  c                }1 a4 n1 m7 T. E3 ?% z. H* q
0 |, W$ b+ \' o+ f: x
        Arrays.sort(ar);
) p& I: q+ d& w4 K; u4 d8 l; l" B& H+ Q7 L
                for(int i=0;i<n;i++)
9 k+ @0 a* Y  @3 J8 }5 c% ?9 a3 j' r% e
                {
% @7 R- z8 }/ ?- P( O8 H5 K4 c7 {: ]  `
                System.out.print(ar[i]+"\n")        & m. H; C+ ~; C5 h# A2 N! u& S) P
5 e- v" V* o3 N9 a
               
6 @1 f' f- Z& g3 B3 l* t$ L
; A' b/ r1 R1 }& }                }8 z: p3 c6 k: s$ a
3 w8 b; r% ]. X& X. L6 I2 l; a" ]" E
        }* B1 m: `' w+ E+ h. i- K
0 Y# \6 {9 X% a8 B: @) L
}
人平

TOP

返回列表