Java107 求平均值

package java實用級;

public class J107 {

    public J107() {
        // TODO Auto-generated constructor stub
    }
    public static void main( String args[] )
    {
          
           try{
             int con = Integer.valueOf(args[0]);
             double tmp=0;
             if(args.length==1){System.out.println("輸入的參數少於兩個");}
             else{
                 for(int i=1;args.length>i;i++){
                     tmp+=Double.valueOf(args[i]);
                 }
                 if(con==0){System.out.printf("平均值 = %.0f",(tmp/(args.length-1)));}
                 else if(con==1){System.out.printf("平均值 = %.1f",(tmp/(args.length-1)));}
                 else if(con==2){System.out.printf("平均值 = %.2f",(tmp/(args.length-1)));}
                 else {System.out.println("第一個參數值輸入錯誤");}
             }
            
           }catch(Exception e){System.out.println("參數輸入錯誤");}
     }
}

沒有留言:

張貼留言