返回列表 發帖

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

本帖最後由 b790113g 於 2011-12-31 09:54 編輯 ; s9 o# N$ D0 ]9 M/ A: B% |
" {' L( Q! u) ~. h% o3 M. E  E
程式中可以輸入值
& e) J, p% f/ U) `& H: {1 y4 L- L2 R( _* [- k. x
實際寫出 jvd1036 ~6 e; [) [  ]2 C5 c& H6 \

1 {* C# m0 v2 x/ N使用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 資料函式引入& E* t: K* D; N% F5 ~) {5 ?
+ F6 o9 B2 c+ ^9 R! P; J
import java.util.Arrays ;4 E' Y8 i' n2 r' d( ?* p

( z& M  I  }9 c7 ]' W# w) e: ~% R! P. ]
& Q$ S2 |; c. x6 K
public class JVD103{+ `7 R! A$ b5 o; x: z
& m( d4 q, r" U7 L4 ~) Q
7 m7 T/ G2 m  I0 h1 z  f
! i( z/ ]. }- E# c
        public static void main(String arg[]){5 H; I5 M, _# e6 D! ~& q! s
3 L% c" r( f$ m7 x; Y
        
2 {. W) M( v$ d# n; {0 l, s) T
$ S( o& v+ v& D6 D- Z" r9 |7 S4 ~        
: [6 p1 _) R  j  o
2 b7 J2 Z9 V5 r2 j                System.out.println("請輸入欲產生之亂數個數:");% q0 O' O; d2 u8 V* g; J

0 S# K$ g5 S% q6 F! \7 F        
6 Y  F; ^1 e( {* f% C
/ ~, j/ K3 Z* b2 Z) p2 k                int n = 0;  //輸入要產生亂數個數 並初始化) ~5 e' x( y, S! ^. r2 V; d' z

) d# v# o" x7 a6 e/ ^% ?               
7 l, s1 G' D) s2 K1 i. C: v' Y! Y; x+ p2 e: T5 J& F
                //使用到io 一定要加上 try catch
- z' |. l& x; o$ O8 I
3 P) ^' z" j7 }% D: A: N                try{
& I+ z( X, v/ z/ P) @  h
5 [1 |" E/ x, }' t2 U: i$ Y% f                        InputStreamReader isr = new InputStreamReader(System.in);  //輸入資料流
: M4 C" }) j4 O7 _6 E$ Z- w) m5 z' P$ Q+ y1 `4 {9 t
                        BufferedReader br = new BufferedReader(isr); //資料暫存區1 v; a! L; w, w/ A
2 m1 A; C& K+ S# f1 s/ C4 T
                        n = Integer.parseInt(br.readLine()); //輸入的資料是String  但我們要的n是int (轉換)
' O8 w2 b. l+ W$ X7 _4 {6 r
5 Y% A, k) O8 W6 M: H( T/ _3 W                }catch(Exception e){}
$ a$ M3 g7 g4 N/ ]0 Q2 i  X6 F5 C! I8 H! I
               
# i  ]7 ~0 A3 p7 |
7 e' q  j3 q& k& g                " I9 |: {2 f! i
7 t: @& v3 ]: b& @1 E8 q5 y7 `
                int Num[] = new int[n];//產生n個亂數的變數
$ L) D0 y& _1 k% [9 O
7 A( Q2 F; k( V                : F0 ^: c" S: T8 e
' d9 q% W$ c3 H3 `" V
                for(int i=0;i<n;i++){
( Z7 d1 C8 S, ~5 m$ L) r5 j$ V1 d0 o# N" o: e6 g% w
                        Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)
0 D2 O  \" y) j0 ]% ^6 _, `2 u( ]6 X* m9 N: D' e& ?# ?+ J
                        //System.out.println(Num[i]);
! N  k- F1 c, {% P! N5 S# G
/ v& }; p+ E0 f, ~+ a, D' i3 P                }
0 h' q( [+ Q) n7 W: K2 K
+ k& C; W: l" O5 y1 s6 m                Arrays.sort(Num); //排序
6 m6 N! C" I8 _; f& T
0 }# Q7 z0 ^  u4 b6 T+ f. j                ) y( c  y5 r8 i3 Z2 p( I

$ z$ R# w4 n2 a0 ?  _# _                //輸出% ^/ n4 J6 s& {7 |! v

) j; ?; D/ U* n; B' `' o' U                for(int i=0;i<n;i++){( e* s4 ~1 c. V9 H5 z* |2 b

9 b0 R3 p* F7 b  R6 _9 m# _% w* \) `7 e5 P                   System.out.print(Num[i]+"\t");
, F; R& A( f  z! j0 B0 L9 m; P, w3 v" P" l
                }; d8 ~$ R6 h% p! {- R* }7 l

0 u, O0 u( h8 u) i: W- L                , _  p* N& x$ n7 d0 ]

$ ~5 x) H  \7 N6 l        }3 }1 l% G* e; {) f: d

, G9 d0 P7 v) g# Q" [$ R8 f8 M9 [# k: c% X/ J

# A  e2 ~  \" o3 E}
人平

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;
3 k, {$ W9 Z( t3 _, g$ j
& ^5 P0 _: _, h' Rimport java.util.Scanner ;1 O. r, ~2 S- r- O7 v
% X8 {! \2 i; u( W' j; a4 _5 J
public class j103
) c" K8 s! ?8 h9 w0 a9 s: n$ G
3 F, }3 m+ k3 c4 F{
& Q6 h- P' \2 X: `# W$ ?  Z. s: \4 @: t( `5 M* N7 n0 A
        public static void main(String[]arg). D8 `) Y6 f# Z( R. }$ r
  O1 o6 p/ `0 U" n
        {% y1 C1 p4 W" E& Y( ?( B# S5 ^
# L4 D* C5 J* @9 @; P
        System.out.println("請輸入欲產生之亂數個數");
0 x, H" \  @: z! N2 a3 ]: F
5 b# Q: m3 n+ H        Scanner s=new Scanner(System.in);" I# ~4 B/ ^+ Y/ J: z

; E' M( G2 ]  s" Q        //System.out.println(s.nextInt());+ r; {) V% `1 X. ~

6 W2 r, D5 P  b        int n = s.nextInt();/ i$ i! g2 q: P- u' D

' u/ j" C+ S9 f. M7 B! l        int ar[]=new int[n];- s5 r. n' n) k  l8 m$ o7 |

6 m' O& M1 k; }% R4 e* W5 k  K0 g                for(int i=0;i<n;i++)4 c1 ]# f3 |. J8 e+ \! k9 Y; H$ g

. K( h- z3 n( }) L/ ]                {  A8 u3 Z1 a: A% j4 ?' s

. ?! J, `( N+ }- U                ar[i]=(int)(Math.random()*1000);$ a6 \1 Z* l2 J5 W6 d. q7 V. B

2 c8 K! J$ |8 p; b) v7 Z                }; `9 o; _) M+ |) H

. I4 u( q0 y9 T  q0 S+ a: y6 V        Arrays.sort(ar);
; \0 f% h2 d: a  ?4 l6 o6 S2 I% c- q' ]; ~! D' @" g9 O4 g6 u
                for(int i=0;i<n;i++)
' ^' ^9 s* M* o- @% C# `% h) u# y! @' h' F; k
                {) Z, h" W2 J: y' n  x7 G
2 ~0 f1 I- F9 D( m) Q5 x4 o
                System.out.print(ar[i]+"\n")        ! W5 a) W) E( B% V* `

/ w6 j  H, k/ Q4 Z, S                0 o6 M' s$ c4 J/ U. Y; y

5 V1 g; }8 R2 m$ l                }4 D" D+ H8 f6 j- {" A$ J

: |5 Y# _7 Q1 q0 c" z6 m        }
0 p  |. e1 D( Y: s/ z8 h% t% C3 d7 C  n6 R& a$ y
}
人平

TOP

返回列表