posts - 495,  comments - 11,  trackbacks - 0

          //用命令行參數

          import java.io.*;
          public class AddArgs
          {
          ??? public static void main(String args[]) throws IOException
          ????? {
          ??????? int sum;
          ??????? try
          ???????? {
          ??????????? sum = Integer.parseInt(args[0])+Integer.parseInt(args[1]);??
          ??????????? System.out.println("兩參數的和是:"+sum);???
          ???????? }
          ??????? catch (Exception e)
          ???????? {??
          ??????????? System.out.println("參數出現錯誤!");
          ???????? }
          ????
          ????? }
          }

          =========================================================

          //用system.in
          import java.io.*;
          public class AddInput
          {
          ??? public static void main(String args[]) throws IOException
          ????? {
          ??????? BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          ??????? int x=0,y=0;
          ??????? try{
          ????????? System.out.println("請輸入第一個整數:");
          ????????? x=Integer.parseInt(br.readLine());
          ????????? System.out.println("請輸入第二個整數:");
          ????????? y=Integer.parseInt(br.readLine());
          ????????? System.out.println("它們的和是:"+(x+y));????????????
          ??????? }
          ??????? catch (Exception e)
          ??????? {
          ????????? System.out.println("輸入有錯誤,請重新運行!");
          ???????? }
          ????? }
          }

          =========================================================

          //用scanner
          import java.io.*;
          import java.util.*;
          public class AddScanner
          {
          ??? public static void main(String args[]) throws IOException
          ??? {
          ??????? Scanner sc=new Scanner(System.in);
          ??????? int x=0,y=0;
          ??????? try
          ??????? {
          ????????? System.out.println("請輸入第一個整數:");
          ????????? x=sc.nextInt();
          ????????? System.out.println("請輸入第二個整數:");
          ????????? y=sc.nextInt();
          ????????? System.out.println("它們的和是:"+(x+y));????????????
          ??????? }
          ??????? catch (Exception e)
          ??????? {
          ????????? System.out.println("輸入有錯誤,請重新運行!");
          ??????? }
          ??? }
          }

          posted on 2007-07-17 00:03 jadmin 閱讀(117) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 乐昌市| 滁州市| 扶余县| 江达县| 年辖:市辖区| 教育| 汝州市| 鄂托克旗| 遂溪县| 图木舒克市| 泰宁县| 万源市| 合山市| 巩留县| 大厂| 江达县| 青川县| 莒南县| 鱼台县| 长兴县| 土默特右旗| 资阳市| 嵊州市| 彭阳县| 福泉市| 垦利县| 都安| 岳阳县| 化德县| 和平县| 隆化县| 全州县| 逊克县| 嵩明县| 兰西县| 涟源市| 仪陇县| 陇川县| 凤翔县| 甘德县| 轮台县|