//用命令行參數
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 閱讀(112)
評論(0) 編輯 收藏