返回列表 發帖

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

本帖最後由 b790113g 於 2011-12-31 09:54 編輯
: @9 V) ]6 {# m4 {( z) _4 H0 P: [9 U  E  m6 D1 \6 y
程式中可以輸入值
: m  n, `# \' ]" ?+ l2 G, I$ l6 N; E
實際寫出 jvd103, [5 c- W  m- U8 M+ _% d  g
- X& e' ^& y; N/ l2 `6 p/ 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 資料函式引入! F4 F7 b. ^  P' z2 ?' B

4 ]6 }0 |, g0 Zimport java.util.Arrays ;" t6 P4 @, B5 W, ^( Q& M  L# y

: F5 o0 F6 t' _" L" Y, h4 e# \) t8 c; t4 n# m9 Q

$ R3 J# m" b- [0 N2 X, P( h6 Zpublic class JVD103{
$ c. _6 r6 i7 ]7 _- [
7 v- n4 ~. B/ @0 ?( I5 i3 \6 D# P- t% E. {( x

; u7 @# }% m! S1 J! _9 t$ |! g        public static void main(String arg[]){, A2 v1 q# J' P1 }6 A
( ~3 K4 V, f7 I# h% e0 \9 R
        
0 s7 x# e% H* D+ r  i3 j' P+ q" d9 K- H
        
+ q4 a& f& Z" L; o8 |# j
) |3 x1 [; j( F" U" |                System.out.println("請輸入欲產生之亂數個數:");
7 m% C' V0 g- k  f) a/ `. x/ }3 \- ~3 [; x1 c. ~5 L, z
        
% D/ Z6 T: s  T! q9 U
. F" Q  V$ b1 u* V                int n = 0;  //輸入要產生亂數個數 並初始化2 y/ L" z# p/ F; i0 c
1 ~; M  q3 ^/ o( K& h% }( E
               
; V0 p" j6 a  ]$ A8 R; x, W- g7 f4 Y, i: S
                //使用到io 一定要加上 try catch- [0 e" s3 l; ]8 Y0 d

* \8 t" u$ b0 x4 L! H                try{0 N5 M1 e/ W# |; D) Z4 f

+ E; @5 i8 _2 D0 a                        InputStreamReader isr = new InputStreamReader(System.in);  //輸入資料流
7 z. p7 O4 L% {% ]- {* |3 m0 a2 X- Q9 Q1 _' W* K5 R
                        BufferedReader br = new BufferedReader(isr); //資料暫存區
, T0 F  l( _3 I7 p2 E" p- F. ?; V% h: s3 S, h/ \9 u# H3 S5 i
                        n = Integer.parseInt(br.readLine()); //輸入的資料是String  但我們要的n是int (轉換)8 i- q% C) U' M

5 W6 c, B; m5 l# G5 w# @                }catch(Exception e){}
. L9 W5 t& H" S1 ^$ r' e4 t& D7 E% ]- [' ^  Q! {+ Y1 S
                & g# q$ w# H4 X5 D1 r8 r% U

  \: ~1 a; v. u9 s1 f  F                9 _' u; i9 I7 B0 D1 w

% b* C/ I. h) O' R' q                int Num[] = new int[n];//產生n個亂數的變數, d# g7 K- g: T& h3 D

* \; `4 W  W5 Q* T' A& z               
  H8 U8 o- X& K# w/ ~4 Q; _% V6 H5 P2 n
                for(int i=0;i<n;i++){
" b# c2 e0 @% ?0 v! p1 K2 H3 j
/ M% _5 v8 h; p- Y                        Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)
" {' x! N- n; ^8 T9 i
/ }8 \' a2 A, [* z; Z2 d. v1 ~                        //System.out.println(Num[i]);
; o' R: {) j# X; a5 L3 V# _/ t/ [. W) ~6 Q; [) V2 g; @" g
                }* {& I6 j% N7 F- y$ _6 ]
9 O# ^5 g6 n8 E  f- P5 M# p- _0 o$ ^( `
                Arrays.sort(Num); //排序. }1 O. R2 ~/ R' ]8 a
! C# w: D" \- E$ K+ I5 V- `! w
               
; w- h" v' R; k) v5 w! x3 x2 k
) o" v) {  l# {6 U3 v; l# R: g                //輸出* D& L: Q0 q4 X7 }

3 x$ u7 l/ \7 X+ w4 S                for(int i=0;i<n;i++){9 S( i  `( I6 }8 T6 |8 G

$ e' ?1 h# x1 d: g& w6 U                   System.out.print(Num[i]+"\t");
* v: G4 G: R+ d9 v- J# w3 H8 C: y
5 c7 \' r0 Q4 d                }0 R6 R0 ~' U# k, I- o" E9 x& B
' F8 i" l! i8 o
               
9 z6 z% F' D' }5 D& k, Z  }, G) W8 R5 S+ s/ E8 J
        }
7 i* o6 w: p5 d. {; ^& E0 @  m
. [7 w/ M1 U  \
6 X0 m" P4 A$ D3 t7 ?. T& g# C; {4 F
}
人平

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;9 n. h1 B1 M  v0 S+ g
6 D7 L/ X# W5 C8 T# d/ X# g& I7 C+ C4 M
import java.util.Scanner ;/ k, I: k3 a1 }8 ~* ^' k
! ?. m4 g  b# ^3 c
public class j103
( C3 W3 M+ ^# `! J* r& ?& x4 g" n; m, n  H3 d) |* W
{8 [: N1 ]; n8 C: f( V) U, |% w" `
" {$ P( b, ^. z, |5 j3 j
        public static void main(String[]arg)/ h: a. V, a1 A" w

4 Q' j/ n. u& A3 f        {
, M* R6 t  u1 n, ?) Z. d1 w, J+ F! {# _$ p$ V' A" b0 A
        System.out.println("請輸入欲產生之亂數個數");
2 ?$ `0 z  L' ?) \( q% X
$ M! `1 Y  _5 k! P1 m        Scanner s=new Scanner(System.in);4 @3 ]5 m1 |# D* [  I: X
6 c' s+ l" S4 i" q5 q4 f' S  J! l7 A
        //System.out.println(s.nextInt());
: u( W# q8 l! f& h/ u1 p6 N# y9 D3 J+ G5 O. P4 L
        int n = s.nextInt();
; h0 z; ?/ v  @3 k: H8 M! o9 @( I5 C, o1 D) c2 a9 m
        int ar[]=new int[n];3 L4 _; Y7 t; f
. ^/ ]* @7 O) P& w% p
                for(int i=0;i<n;i++)
4 ^* R7 g3 ?' y
0 d; M4 o2 o) T4 N4 J3 s1 s                {
) e# V2 E! Z$ @- D; B# P8 a% d0 S' L& x, W
                ar[i]=(int)(Math.random()*1000);
! G6 z4 D' I4 E
. M# m9 m# ~) E6 X+ ^; |5 I) t                }
) N+ U% ]% l0 `, e/ [+ E* P  s1 s+ u( k' ^" _
        Arrays.sort(ar);. T7 F3 t8 \# j$ l

" A* n- e1 J$ J9 l- g$ A% r/ h  r/ a                for(int i=0;i<n;i++)' |  _. v; i7 ~

7 N! T' U) ?/ Q8 |$ f! ]                {4 V% q- n: ^& L1 ~' H3 R- v
/ R) S$ P# e4 q: N
                System.out.print(ar[i]+"\n")        
' r3 {/ _1 D- k7 j. l9 ?+ m( F2 h9 ~  N3 s4 Q( s2 o
                4 k) P; b- E! _6 ^' @& G5 p
& ^/ S4 N% }  l# }% e" z
                }( J7 C2 n/ `# q0 w
8 p" f& z) q) [9 ?$ v; y  s  ]: k
        }; b. R+ ^: `9 o( U: S: G& Q
; i$ e( p6 u6 ]2 O7 F8 U/ ~0 I
}
人平

TOP

返回列表