1 / 9

数组及应用

数组及应用. 模块化程序设计. 例:为奥运会设计体操比赛的打分程序. 累加求总分. 求出最高分. 求出最低分. 输出数据. 输入数据. 输出结果.   根据比赛打分规则的要求,可以将打分程序设计为以下几个模块。. 模块1. 模块 2. 求最后得分. 模块 4. 模块 5. 模块 3. // 比赛打分成绩的统计 import java.io.*; public class Pingfen_p70{ static int n; // 声明变量 n ,保存评委人数

chacha
Télécharger la présentation

数组及应用

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 数组及应用

  2. 模块化程序设计 例:为奥运会设计体操比赛的打分程序 累加求总分 求出最高分 求出最低分 输出数据 输入数据 输出结果   根据比赛打分规则的要求,可以将打分程序设计为以下几个模块。 模块1 模块2 求最后得分 模块4 模块5 模块3

  3. //比赛打分成绩的统计 import java.io.*; public class Pingfen_p70{ static int n; //声明变量n,保存评委人数 static double a[]; //声明数组a,保存评分 static double sm,max,min; //声明变量sm、max、min //输入数据模块(略) //输出评分情况模块(略) //分数累加模块(略) //求最高分模块(略) //求最低分模块(略) //主函数 public static void main(String args[])throws IOException{ inputArray(); //调用输入数据模块 outputArray(); //调用输出评分情况模块 sum(); //调用方法累加模块 maxOfArray(); //调用方法求最大值模块 minOfArray(); //调用方法求最小值模块 double fen=(sm-max-min)/(n-2); //平均分 System.out.println("该选手的最后得分:"+fen); //输出得分 } } 比赛打分程序

  4. //比赛打分成绩的统计 import java.io.*; public class Pingfen_p70{ static int n; //声明变量n,保存评委人数 static double a[]; //声明数组a,保存评分 static double sm,max,min; //声明累加变量sm、最大值变量max、最小值变量min static void inputArray() throws IOException //调用输入数据模块 { //使用缓冲区(BuffeReader)从文本数据流读取文本数据 InputStreamReader reader=new InputStreamReader(System.in); BufferedReader input=new BufferedReader(reader); System.out.println("输入评委人数:"); //提示输入信息 String s=input.readLine(); //从键盘输入一个字符串 n=Integer.parseInt(s); //将字符串s转换为整数(评委人数) String ss[]=new String[n]; //初始化字符串数组对象ss a=new double[n]; //初始化数组对象a for(int i=0;i<n;i++) { System.out.println((i+1)+"> 输入评分:"); //提示输入信息 ss[i]=input.readLine();//从键盘输入一个字符串 double x=Double.parseDouble(ss[i]);//将字符串ss转换为浮点数 a[i]=x; //然后保存到数组a中 } } 条件运算符(if语句) 比赛打分程序 3

  5. 比赛打分程序 //比赛打分成绩的统计 import java.io.*; public class Pingfen_p70 { static int n; //声明变量n,保存评委人数 static double a[]; //声明数组a,保存评分 static double sm,max,min; //声明变量sm、max、min //输入数据模块(略) //输出评分情况模块 static void outputArray() //输出数组 { for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } System.out.println(); } //主函数(略) }

  6. 比赛打分程序 //比赛打分成绩的统计 import java.io.*; public class Pingfen_p70 { static int n; //声明变量n,保存评委人数 static double a[]; //声明数组a,保存评分 static double sm,max,min; //声明变量sm、max、min //输入数据模块(略) //输出评分情况模块(略) //分数累加模块 static void sum() //分数累加 { for(int i=0;i<a.length;i++) { sm=sm+a[i]; //累加 } } //主函数(略) }

  7. 比赛打分程序 3 //比赛打分成绩的统计 import java.io.*; public class Pingfen_p70 { static int n; //声明变量n,保存评委人数 static double a[]; //声明数组a,保存评分 static double sm,max,min; //声明变量sm、max、min //输入数据模块(略) //输出评分情况模块(略) //分数累加模块(略) //求最高分模块 static void maxOfArray() { max=a[0]; //将max逐一与数组其它元素比较,把最大值存入max for(int i=0;i<a.length;i++) { if(max<a[i]) max=a[i]; } } //主函数(略) }

  8. 比赛打分程序 //比赛打分成绩的统计 import java.io.*; public class Pingfen_p70{ static int n; //声明变量n,保存评委人数 static double a[]; //声明数组a,保存评分 static double sm,max,min; //声明变量sm、max、min //输入数据模块(略) //输出评分情况模块(略) //分数累加模块(略) //求最高分模块(略) //求最低分模块(略) static void maxOfArray() { } //主函数(略) }

  9. 比赛打分程序 例:产生n个随机数. String s=input.readLine(); int n=Integer.praseInt(s); int a[ ]=new int[ n ]; for(int i=0;i<n;i++) { a[i]=(int)(Math.random()*90+10); }

More Related