一个简单的数组算法实例。该算法用到了一个引用类,因此必须先编译出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
...{
s = 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;
}
}
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
...{
s = 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的值.");
}
}
}
// 程序名称: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的值.");
}
}
}