2011年7月7日 星期四

2011-07-07 JAVA

http://tw.zkoss.org/  Firamework
1.http://tw.zkoss.org Firamework




// 學習月份的英文單字的程式



import java.util.Random;

import java.util.Scanner;



class MonthCAI {



public static void main(String[] args) {

Random rand = new Random();

Scanner stdIn = new Scanner(System.in);

String[] monthString = {

"January", "February", "March", "April", "May", "June", "July",

"August", "September", "October", "November", "December"

}; //另一空間起始記憶體位址



int month = rand.nextInt(12); // 對應的月份:0~11的亂數

System.out.println("問題為" + monthString[month]);



int c=0;

while (true) {

System.out.print("這是幾月呢:");

int m = stdIn.nextInt();



if (m == month + 1)

break;

else {

++c;

System.out.println("答錯了。");

}

if (c>=2)



System.exit(1); //1退出

}

System.out.println("正確答案。");

}

}

-----------------------------------------------------------------------



// 顯示陣列變數的值



class PrintArray {



public static void main(String[] args) {

int[] a = new int[5];

System.out.println("a = " + a);



a = null; //清除記憶體位址

System.out.println("a = " + a);

}

}



-------------------------------------------------------------



// 合計2列3行的矩陣



class Matrix_test {



public static void main(String[] args) {

int[][] x = { {0, 2, 0}, {1, 0, 1} ,{1,1,1}};





for (int i = 0; i < x.length; ++i) {

for (int j = 0; j < x[i].length ; ++j)

//c[i][j] = a[i][j] + b[i][j];



System.out.println(x[i][j]==1 ? '*' : ' ');

}





}



}



-------------------------------------------------------------



//網路抓檔



import java.net.*;

import java.io.*;



public class GetURL1 {

public static void main(String argv[]) throws Exception {



URL url = new URL("http://140.137.222.71/abc.txt"); //URL.class

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); //建立連線;讀進檔案



String line;

StringBuffer sb = new StringBuffer();



while ((line = in.readLine()) != null) {

sb.append(line+"\n");

}



in.close();

System.out.println(sb.toString());

}

}



----------------------------------------------------------------------------
import java.net.*;


import java.io.*;



public class GetURL3 {

public static void main(String argv[]) throws Exception {



URL url = new URL("http://140.137.222.50/abc.txt"); //URL.class

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));


int v=0;

String line;

while ((line = in.readLine()) != null) {

for (int i=0; i < line.length() ; i++) {

char c = line.charAt(i); //字串比較

if ( c >=48 && c<=57 )

//System.out.print(c);

v++;

}

}System.out.println(v);



in.close();

//System.out.println(c);

}

}
---------------------------------------------------------------
import java.net.*;


import java.io.*;



public class GetURL4 {

public static void main(String argv[]) throws Exception {



URL url = new URL("http://140.137.222.50/abc1.txt"); //URL.class

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));



String line;

while ((line = in.readLine()) != null) {


String[] a1 = line.split(":");

System.out.println(a1[1]);


}


in.close();

//System.out.println(c);

}

}
--------------------------------------------------------------
import java.net.*;


import java.io.*;


public class GetURL5 {

public static void main(String argv[]) throws Exception {

URL url = new URL("http://140.137.222.50/abc2.txt"); //URL.class

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

//String s ;

//String[] a1 = s.split(":");

//System.out.println(a1[1]);

int sum=0;

String s;

while ((s = in.readLine()) != null) {

if (s.isEmpty() == false) {

String[] a1 = s.split(":");

int t=Integer.parseInt(a1[1]);



sum+= t ;

}

} System.out.print(sum);

in.close();

//System.out.println(c);

}

}
----------------------------------------------------------------
String x = "123"; //必需為數字,否則不執行parseInt()

int y = Integer parseInt(x); //字串轉型數值int
-----------------------------------------------------------------
White Space

ASC 10,13 (換行)
ASC 9 (TAB)
ASC 32 (空白)

沒有留言:

張貼留言