java求最高分 最低分_用java编程实现求八个学生某门课的最高分最低分和平均分...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

/*

* B.java

*

* Created on 2007年4月18日, 下午7:58

*

*/

package edu.whu.idle.baidu;

/**

*

* @author idle~

*/

public class B {

private int num = 0; // 学生人数

private double maxScore = 0.0;

private double minScore = 0.0;

private double aveScore = 0.0;

private double tolScore = 0.0;

/** Creates a new instance of B */

public B(int num) {

this.num = num;

java.util.Scanner scanner = new java.util.Scanner(System.in);

System.out.println("please input " + num + " students' score");

maxScore = minScore = aveScore = scanner.nextDouble();

System.out.println("score of student 1 is" + maxScore);

for(int i = 0;i 

double temp = scanner.nextDouble();

System.out.println("score of student " + (num+2) + "is" + temp);

tolScore += temp;

if(maxScore 

maxScore = temp;

if(minScore > temp)

minScore = temp;

}

aveScore = tolScore / num;

System.out.println("max Score is " + maxScore);

System.out.println("min Score is " + minScore);

System.out.println("average score is " + aveScore);

}

public static void main(String[] args) {

new B(8);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那我来给你提供一个Java的MapReduce程序实现,主要使用Hadoop框架。 先看Mapper类的实现: ```java import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class ScoreMapper extends Mapper<Object, Text, Text, IntWritable>{ private Text course = new Text(); private IntWritable score = new IntWritable(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String[] fields = value.toString().split(","); course.set(fields[0]); // 程名作为Map输出的key score.set(Integer.parseInt(fields[1])); // 成绩作为Map输出的value context.write(course, score); } } ``` 上述代码中,我们使用了Hadoop提供的Mapper类,并重写了其map方法,实现了Map函数的逻辑。具体来说,我们将输入数据按照逗号进行割,将程名作为Map输出的key,将成绩作为Map输出的value,最后使用context.write方法输出键值对。 接下来看Reducer类的实现: ```java import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class ScoreReducer extends Reducer<Text, IntWritable, Text, Text> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; int sum = 0; int count = 0; for(IntWritable value : values) { int score = value.get(); max = Math.max(max, score); min = Math.min(min, score); sum += score; count++; } double avg = (double) sum / count; String result = "max: " + max + ", min: " + min + ", avg: " + avg; context.write(key, new Text(result)); } } ``` 上述代码中,我们使用了Hadoop提供的Reducer类,并重写了其reduce方法,实现了Reduce函数的逻辑。具体来说,我们对每个键值对的值进行遍历,并记录最大值、最小值、总和和数量,最后计算平均值并输出结果。 最后,我们需要在Driver类中进行MapReduce程序的配置和运行: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class Score { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "score"); job.setJarByClass(Score.class); job.setMapperClass(ScoreMapper.class); job.setReducerClass(ScoreReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 上述代码中,我们使用了Hadoop提供的Job类,并对MapReduce程序进行了配置,包括输入路径、输出路径、Mapper类、Reducer类等。最后,我们使用job.waitForCompletion方法将MapReduce程序提交到集群中运行。 以上就是一个基本的Java的MapReduce程序实现,可以用于统计每门科目的最高分最低平均分
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值