2011年7月4日 星期一

2011-07-04 JAVA

// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class LinearSearch {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        final int n = 12;        // 元素數
        int[] a = new int[n];        // 宣告陣列

        for (int j = 0; j < n; j++)
            a[j] = rand.nextInt(10);

        System.out.print("陣列a的所有元素的值\n{ ");
        for (int j = 0; j < n; j++)
            System.out.print(a[j] + " ");
        System.out.println(" }");

        System.out.print("搜尋的數值:");
        int key = stdIn.nextInt();

        int i;
        for (i = 0; i < n; i++)
            if (a[i] == key)
                break;

        if (i < n)                            // 搜尋成功
            System.out.println("該值存在於a[" + i + "]。");
        else                // 搜尋失敗
            System.out.println("該值不存在。");           
    }
}
----------------------------------------------------------------------------------
// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class LinearSearch {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        final int n = 12;        // 元素數
        int[] a = new int[n];        // 宣告陣列

        for (int j = 0; j < n; j++)
            a[j] = rand.nextInt(10);

        System.out.print("陣列a的所有元素的值\n{ ");
        //for (int j = 0; j < n; j++)
            //System.out.print(a[j] + " ");
        //System.out.println(" }");
        for (int j : a)
           System.out.print(j + " ");
          
        System.out.print("搜尋的數值:");
        int key = stdIn.nextInt();

        int i;
        for (i = 0; i < n; i++)
            if (a[i] == key)
                break;

        if (i < n)                            // 搜尋成功
            System.out.println("該值存在於a[" + i + "]。");
        else                // 搜尋失敗
            System.out.println("該值不存在。");           
    }
}
-------------------------------------------------------------------------------
// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class MyP6_7 {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        final int n = 12;        // 元素數
        int[] a = new int[n];        // 宣告陣列

        for (int j = 0; j < n; j++)
            a[j] = rand.nextInt(10);

        System.out.print("陣列a的所有元素的值\n{ ");
        //for (int j = 0; j < n; j++)
            //System.out.print(a[j] + " ");
        //System.out.println(" }");
        for (int j : a)
           System.out.print(j + " ");
         
        System.out.print("搜尋的數值:");
        int key = stdIn.nextInt();

        int i;
        for (i = n-1; i >= 0; i--)  //倒回來找
            if (a[i] == key)
                break;

        if (i < n)                            // 搜尋成功
            System.out.println("該值存在於a[" + (i+1) + "]。");
        else                // 搜尋失敗
            System.out.println("該值不存在。");          
    }
}
------------------------------------------------------------------------------------
// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class MyP6_7 {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        final int n = 12;        // 元素數
        int[] a = new int[n];        // 宣告陣列

        for (int j = 0; j < n; j++)
            a[j] = rand.nextInt(10);

        System.out.print("陣列a的所有元素的值\n{ ");
        //for (int j = 0; j < n; j++)
            //System.out.print(a[j] + " ");
        //System.out.println(" }");
        for (int j : a)
           System.out.print(j + " ");
         
        System.out.print("搜尋的數值:");
        int key = stdIn.nextInt();

        int i;
        for (i = n-1; i >= 0; i--)  //倒回來找
            if (a[i] == key)
                break;

        if (i < n && i!=-1)                            // 搜尋成功
            System.out.println("該值存在於a[" + (i+1) + "]。");
        else                // 搜尋失敗
            System.out.println("該值不存在。");          
    }
}
----------------------------------------------------------------------------------------------
// 將陣列元素的順序逆向重整並顯示

import java.util.Random;
import java.util.Scanner;

class ReverseArray {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        System.out.print("元素數:");
        int n = stdIn.nextInt();                // 輸入元素數
        int[] a = new int[n];                    // 宣告陣列

        for (int i = 0; i < n; i++) {
            a[i] = 10 + rand.nextInt(90);
            System.out.println("a[" + i + "] = " + a[i]);
        }

        for (int i = 0; i < n / 2; i++) {
            int t = a[i];
            a[i] = a[n - i - 1];     //a[i]與a[n-i-1] swap交換
            a[n - i - 1] = t;
        }

        System.out.println("將元素的排列順序逆轉。");
        for (int i = 0; i < n; i++)
            System.out.println("a[" + i + "] = " + a[i]);
    }
}
-----------------------------------------------------------------------------------

// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class MyP6_12 {

    public static void main(String[] args) {
        int[] a = {0,1,2,3,4,5,6,7,8,9};
       
        for(int i=0 ; i<10 ; ++i){   //i<10,跑幾次,不一定是10
           int r = rand.nextInt(9);  //0~8
          
           int t;
           t=a[r];
           a[r]=a[r+1];
           a[r+1]=t;
          
          
        }
           for (int v :a)
              System.out.print(v+",");

     }   

   
   
    }
----------------------------------------------------------------------------
/**
 * 處理檔案及目錄
 *
 * @author (Shieh-Fu Chen)
 * @version (V 1.0  2011/06/27)
 *
 * 執行結果
 * ======================================================================
 * CD=D:\bobe\toBalaTech\Develop\Java\java2009\tools\npp587
 * D:\\fred.txt
 */
import java.util.Scanner;
import java.io.*;
public class MyFile2 {
   public static void main (String args[]) throws IOException   {

     System.out.println("user.dir = " + System.getProperty("user.dir"));  // JVM環境變數;取得 user.dir 環境變數的值
     System.setProperty("user.dir", "D:/");                       // 改變 user.dir 環境變數的值
       
     // java.io.File 這類別主要是處理檔案及目錄的 PathName, 而不是處理檔案的 I/O (開檔, 讀檔, 存檔),
     // 下式只是取得 fred.txt 完整目錄名稱, 並沒有建立新檔
     String s = new File("fred.txt").getAbsolutePath();  //File class;檔案不存在一樣成立
     System.out.println(s);
   
     if (new File(s).exists()) {
        Scanner stdIn = new Scanner(System.in);
       
        System.out.println("進行倒數: ");
        int x;
       
        System.out.print("正整數值: ");
        x = stdIn.nextInt();
       
        if (x == 0)
          System.out.println(new File(s).delete());
     }
       System.out.println("檔案不存在");
   }
}
------------------------------------------------------------------------------
/**
 * 建立文字檔, 並寫入字串資料
 *
 * @author (Sung-Lin Chen)
 * @version (V 0.1  2011/04/22)
 */

import java.util.Scanner;
import java.io.*;

class MyWriter1 {
   public static void main(String[] argv) throws IOException {
  
      // PrintWriter 沒有提供 Append File 的建構子     
      PrintWriter pWriter = new PrintWriter("d:\\MyWriter1.txt"); //PrintWriter產生一檔案,如有`存在檔案則將內容清空
      Scanner stdIn = new Scanner(System.in);

      System.out.print("ID:");
      String s = stdIn.nextLine(); //nextLine輸入字串,空白亦可

      System.out.print("Score:");
      int a = stdIn.nextInt();                       

      // 寫入檔案
      pWriter.print(s+",");
      pWriter.print(a);

      // 關閉檔案
      pWriter.close();
   }
}
-------------------------------------------------------------------------------
/**
 * 將輸入成績, 附加在檔案後面
 *
 * @author (Sung-Lin Chen)
 * @version (V 0.1  2011/04/22)
 *
 */

import java.util.Scanner;
import java.io.*;

class MyWriter2 {
   public static void main(String[] argv) throws IOException{

        // FileWriter 有提供 Append File 的建構子, 但是寫入的方法 (write()) 只提供字元及字串資料型態
        FileWriter fWriter = new FileWriter("d:\\MyWriter2.txt", true);  //FileWriter-->true資料附加
       
          Scanner stdIn = new Scanner(System.in);

        System.out.print("Name:");
        String n = stdIn.nextLine();

        System.out.print("Address:");
        String a = stdIn.nextLine();                       
       
        fWriter.write(n+",");   // write() 無法寫入 double 資料
        fWriter.write(a);
        fWriter.write("\n");   // 在 記事本 中, 並沒有換行, 應如何解決 ?在檔案是以ASC碼的10,13 (微軟),其它OS為10              

        fWriter.close();

   }
}
----------------------------------------------------------------------------------------------
/**
 * 輸入五位同學, 國英數三科成績,存入文字檔
 *
 * @author (Sung-Lin Chen)
 * @version (V 0.1  2011/04/22)
 *
 */

import java.util.Scanner;
import java.io.*;

class MyWriter3 {
   public static void main(String[] argv) throws IOException {

        // FileWriter 有提供 Append File 的建構子
        FileWriter fWriter = new FileWriter("d:\\MyWriter3.txt", true);
      
        // PrintWriter 有提供好用的寫入方法 print()
        PrintWriter pWriter = new PrintWriter(fWriter);
      
          Scanner stdIn = new Scanner(System.in);

        System.out.print("學號:");
        String n = stdIn.nextLine();

        System.out.print("國文:");
        int c = stdIn.nextInt();   
      
        System.out.print("英文:");
        int e = stdIn.nextInt();
      
        System.out.print("數學:");
        int m = stdIn.nextInt();
      
        pWriter.print(n+","); // 寫入字串
        pWriter.print(c+","); // 寫入整數
        pWriter.print(e+","); // 寫入整數
        pWriter.print(m);     // 寫入整數
      
        // 根據不同作業系統, 寫入正確的換行碼
        pWriter.print(System.getProperty("line.separator")); //取得JVM環境變數           

        pWriter.close();
   }
}
-----------------------------------------------------------------------------------------------------------
/**
 * 讀出文字檔內容
 *
 * @author (Sung-Lin Chen)
 * @version (V 0.1  2011/04/22)
 */

import java.io.*;

public class MyReader1 {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("d:\\MyWriter2.txt"); //FileReader讀入檔案;如檔案不存在, 則無法RUN;
        BufferedReader bReader = new BufferedReader(fileReader); //緩衝;提供很好用的METHOD readLine()
       
        String s;
        while((s = bReader.readLine()) != null ) {
           System.out.println(s);
        }
        bReader.close();
    }
}
----------------------------------------------------------------------------------------------------
import java.util.Scanner;
import java.io.*;
class MyReaderWriter {
     public static void main(String[] args)throws IOException{
        FileWriter fWriter = new FileWriter("d:\\out.txt", false);
        FileReader fileReader = new FileReader("d:\\MyWriter2.txt");
        BufferedReader bReader = new BufferedReader(fileReader);
       
       
     String s;   
        while((s = bReader.readLine()) != null ) {
           for (int i=0; i < s.length() ; i++) {
             char c = s.charAt(i);
             fWriter.write(c==',' ? '$' : c);
             /* /* if ( c == ',' )
                System.out.print('$');
             else
                System.out.print(c); */
           }     
           fWriter.write(System.getProperty("line.separator"));   
        }
       
        fileReader.close();
        fWriter.close();       

}
}
-------------------------------------------------------------------
/**
 * 讀出文字檔內容, 並解析單行文字
 *
 * @author (Shieh-Fu Chen)
 * @version (V 0.1  2011/07/04)
 *
 */

import java.io.*;

public class dafumenu {
    public static void main(String[] args) throws IOException{

        FileReader fileReader = new FileReader("d:\\dafumenu.txt");
        BufferedReader bReader = new BufferedReader(fileReader);
       
        String s;   
        while((s = bReader.readLine()) != null ) {
            if (s.equals("[system]"))
              System.out.println("system");
            else if (s.equals("[account]"))
              System.out.println("account");
            else if (s.equals("[student]"))
              System.out.println("student");
            else
               System.out.println();
           }   
              fileReader.close();
        }
       
        //fileReader.close();
   
}

沒有留言:

張貼留言