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 閱讀(116) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 澄迈县| 沧州市| 西安市| 静乐县| 仁布县| 湘乡市| 泸西县| 怀宁县| 广水市| 武陟县| 紫阳县| 顺平县| 阿拉善右旗| 南乐县| 平邑县| 桐城市| 颍上县| 抚顺县| 香格里拉县| 体育| 汉源县| 绥宁县| 承德市| 西峡县| 临城县| 东安县| 长海县| 广水市| 武功县| 通化县| 自治县| 乌兰察布市| 揭西县| 高阳县| 全南县| 岳池县| 新竹市| 朝阳区| 库伦旗| 怀柔区| 肃宁县|