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 阅读(111) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: