html教程菜鸟教程 编写一个程序,从键盘输入5个学生的分数(以非数字字符串结束),然后输出高于平均分的分数?
编写一个程序,从键盘输入5个学生的分数(以非数字字符串结束),然后输出高于平均分的分数?
import java.util.*
* @author young
*
*/
public class He {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in)
String reg="^[0-9] $"
float[] s = new float[5]
int count = 0
float avg = 0, sum = 0
System.out.println("输入5个分数,以非数字结束:")
while(true){
String f = sc.nextLine()
if(f.matches(reg)){
s[count] = Integer.parseInt(f)
}else {
break
}
count
}
for(int j=0j<s.lengthj ){
sum =s[j]
avg=sum/s.length
}
System.out.println("平均分是:" avg)
System.out.print("大于平均分的分数是: ")
for(float temp : s){
if(temp > avg){
System.out.print(" " temp)
}
}
}
}
html教程菜鸟教程 java生成指定范围的随机数 数组中大于平均值的个数
版权声明:本文内容由互联网用户自发贡献,本站不承担相关法律责任.如有侵权/违法内容,本站将立刻删除。