本帖最後由 b790113g 於 2011-12-31 09:54 編輯
. T3 z% Q" T- e0 e: K; P0 p
1 o1 `8 u' K, Q( J& \* c* A程式中可以輸入值9 R2 b5 m* {% [% n/ G, ]
, ?! Z- B0 s( X, i9 a* o1 t h
實際寫出 jvd103
- F ]% x7 {4 M4 `1 j6 ]& Q7 G2 O* I
8 r- y4 ]$ a0 t& h使用io一定要使用 try catch- import java.io.* ; // input output 資料函式引入
- import java.util.Arrays ;
- public class JVD103{
- public static void main(String arg[]){
-
-
- System.out.println("請輸入欲產生之亂數個數:");
-
- int n = 0; //輸入要產生亂數個數 並初始化
-
- //使用到io 一定要加上 try catch
- try{
- InputStreamReader isr = new InputStreamReader(System.in); //輸入資料流
- BufferedReader br = new BufferedReader(isr); //資料暫存區
- n = Integer.parseInt(br.readLine()); //輸入的資料是String 但我們要的n是int (轉換)
- }catch(Exception e){}
-
-
- int Num[] = new int[n];//產生n個亂數的變數
-
- for(int i=0;i<n;i++){
- Num[i] = (int)(Math.random()*1000) ; //double int 都是數值,差別在小數點 (int)
- //System.out.println(Num[i]);
- }
- Arrays.sort(Num); //排序
-
- //輸出
- for(int i=0;i<n;i++){
- System.out.print(Num[i]+"\t");
- }
-
- }
- }
複製代碼- import java.util.Scanner; //輸出入
- import java.util.Arrays; //陣列
- public class jva103{
- public static void main(String args[]){
-
- System.out.println("請輸入欲產生之亂數個數:");
- Scanner s = new Scanner(System.in);
- //System.out.println(s.nextInt());
-
- int n = s.nextInt();
- int ar[] = new int[n];
-
- //產生亂數
- for(int i=0;i<n;i++){
- ar[i] = (int)(Math.random()*1000) ;
- //System.out.println(ar[i]);
- }
- Arrays.sort(ar);
-
- for(int i=0;i<n;i++){
- System.out.print(ar[i]+"\t");
- }
-
- }
- }
複製代碼 |