利用数组计算平均值

 一个简单的数组算法实例。该算法用到了一个引用类,因此必须先编译出ConsoleReader类放到同目录地下才能通过。源程序一并给出。
源程序: ConsoleReader.java

package ConsoleReader;
import  java.io.
* ;

public   class  ConsoleReader 
{
private BufferedReader  reader;

    
public ConsoleReader(InputStream Input_Out)
{
reader 
= new BufferedReader(new InputStreamReader(Input_Out));
}

    
public int readInt()
   
//throws IOException,NumberFormatException
{
     
int returnInt = 0;
     String s;
  
try
  
{

        s 
= reader.readLine();
        returnInt 
= Integer.parseInt(s);
      
//if( s.length() == 0 )
      
//   throw new NumberFormatException();
  }

     
catch(IOException eIO)
     
{
      System.err.println(
"Input Exception!");
      System.err.println();
      System.exit(
1);
     }

     
catch(NumberFormatException eNF)
     
{
      System.err.println(
"Number Format Exception!");
      System.err.println(
"Not input empty!");
      System.err.println(
"Please input Integer number!");
      System.err.println();
      System.exit(
1);
     }

     
return returnInt;
}


public double readDouble()
   
//throws IOException,NumberFormatException
{
     
double returnDouble = 0;
     String s;
  
try
  
{

= reader.readLine();
      returnDouble 
= Double.parseDouble(s);
      
//if( s.length() == 0 )
      
//   throw new NumberFormatException();
  }

     
catch(IOException eIO)
     
{
      System.err.println(
"Input/Output Exception!");
      System.err.println();
      System.exit(
1);
     }

     
catch(NumberFormatException eNF)
     
{
      System.err.println(
"Number Format Exception!");
      System.err.println(
"Not input empty!");
      System.err.println(
"Please input Double number!");
      System.err.println();
      System.exit(
1);
     }

     
return returnDouble;
}

////
   public String readLine()
    
{  
    String inputLine 
= "";
    
       
try
       
{
       inputLine 
= reader.readLine();
       }

       
catch(IOException e)
       
{
       System.err.println(
"Input/Output Exception!");
       System.err.println();
       System.exit(
1);
       }

   
catch(NumberFormatException eNF)
     
{
      System.err.println(
"Number Format Exception!");
      System.err.println(
"Not input empty!");
      System.err.println(
"Please input String!");
      System.err.println();
      System.exit(
1);
     }


       
return inputLine;
    }

    
}

数组平均值计算代码: Array01.java

// ============= Program Description ===============
// 程序名称:Array01.java
// 程序目的:运用一维数组设计一个可就算多个数的平均的程序
// Written By Mildo .
// =================================================

import  ConsoleReader. * ;
public   class  Array01
{
    
public static int i; //for 循环计数变量
    public static float Number[] = new float[20]; //存储数据的浮点数数组
    public static float Summary; //数据总和变量
    public static float Average; //数据平均变量
    
    
public static void main(String[] args)
    
{
        Summary 
= 0//数据总和初始化为0
        System.out.println("请输入数组的最大个数:");
        
//读入最大可输入数据个数
        ConsoleReader console = new ConsoleReader(System.in);
        
int Max = console.readInt();
        System.out.println(
""); //换行
        if(Max<=20)
        
{
            
for(i=0;i<Max;i++)
            
{
                
int j = i+1;
                System.out.println(
"请输入第"+j+"个值!");
                Number[i] 
= (float)console.readDouble();
                Summary 
+= Number[i];
            }

            Average 
= Summary / Max; //平均值计算
            System.out.println("该数组的平均值是: "+Average);
        }

        
else
        
{
            
//数据超出范围
            System.out.println("++出错了,请输入一个小于20的值.");
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值