java分数代码 java学生成绩代码
如何用java编写一个,手动输入10个分数,去掉一个最高分,去掉一个最低分,并把剩余的数求平均值的的
输出结果图:
创新互联基于分布式IDC数据中心构建的平台为众多户提供四川移动机房托管 四川大带宽租用 成都机柜租用 成都服务器租用。
参考代码如下:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class xxx {
public static void main(String[] args) {
System.out.println("请输入10个数字,以逗号隔开:");
Scanner scanner=new Scanner(System.in);
String input=scanner.next();
//将输入的数字按照","分割成数组
String[] numStrs=input.split(",");
ListInteger nums=new ArrayListInteger();
//将数组的数字放入集合list中方便排序和取出最大最小数
for (int i = 0; i numStrs.length; i++) {
nums.add(Integer.parseInt(numStrs[i]));
}
Collections.sort(nums);
System.out.println("从小往大排序后:"+nums);
nums.remove(0);
nums.remove(nums.size()-1);
System.out.println("去掉最小数和最大数后:"+nums);
double avg=0d;
int sum=0;
//求出剩下数字的总和
for (int i = 0; i nums.size(); i++) {
sum+=nums.get(i);
}
//求平均值(总和/剩下数字的个数)
avg=sum/(nums.size());
System.out.println("["+nums+"]的平均数为:"+avg);
}
}
JAVA代码问题!输入5名学生的成绩,并计算总成绩,平均分,最高分,最低分
public static void main(String[] args) {
double scores[] = new double[5];
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=0;
String inputStr=null;
System.out.println("请输入5名学生的成绩:");
Scanner input = new Scanner(System.in);
while(count5){
try{
if(count 5){
System.out.println("请输入第"+(count+1)+"个分数:");
}
inputStr=input.nextLine();
scores[count++]=Double.valueOf(inputStr.trim());
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim())){
System.out.println("您已成功结束程序");
System.exit(0);
}
System.out.println("若想结束请输入:exit");
System.out.print("您输入的分数不是数值类型,");
count--;
}
}
input.close();
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("总成绩是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
//-------------------------------------------------------------------------
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
Double[] scores = null;
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=1;
ListDouble inputScores=new ArrayListDouble();
String inputStr=null;
System.out.println("请输入要统计学生的成绩(理论上可以输入无限个,前提是你有那么大的内存):");
while(true){
try{
System.out.println("请输入第"+count+++"个分数,或输入ok进行计算,离开请输入exit");
inputStr=input.nextLine();
inputScores.add((double)Double.valueOf(inputStr.trim()));
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim().toLowerCase())){
System.out.println("您已成功结束程序");
input.close();
System.exit(0);
}
if(inputStr!=null "ok".equals(inputStr.trim().toLowerCase())){
break;
}
System.out.println("您输入的分数不是数值类型,");
System.out.println("若想结束请输入exit ,若想计算结果请输入ok");
count--;
}
}
if(inputScores.size()==0){
System.out.println("您没有输入学生成绩,无数据可统计,程序结束。");
return ;
}
scores=inputScores.toArray(new Double[inputScores.size()]);
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("总成绩是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
}
用java怎么写分数?
你可以写个分数类,下面是参考:
public class Fraction {
private Integer numerator; // 分子;
private Integer denominator; // 分母
/**
* 构造器
*/
public Fraction() {
}
public Fraction(Integer numerator, Integer denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public Integer getDenominator() {
return denominator;
}
public void setDenominator(Integer denominator) {
this.denominator = denominator;
}
public Integer getNumerator() {
return numerator;
}
public void setNumerator(Integer numerator) {
this.numerator = numerator;
}
/**
* 求最大公约数
*
* @return
*/
private int getGCD(int a, int b) {
int max = Math.max(a, b);
int min = Math.min(a, b);
int mod = max % min;
if (mod == 0) {
return min;
} else {
return this.getGCD(mod, min);
}
}
/**
* 简化和约分
*
* @return
*/
public Fraction simplify() {
// 格式化
if (this.numerator == 0) {
this.denominator = null;
return this;
} else if (this.denominator * this.numerator 0
this.denominator 0) {
this.denominator = Math.abs(this.denominator);
this.numerator = Math.abs(this.numerator);
return this;
} else if (this.denominator 0) {
this.denominator = Math.abs(this.denominator);
this.numerator = -this.numerator;
return this;
}
// 约分
int gcd = this.getGCD(Math.abs(denominator), Math.abs(numerator));
if (gcd == 1) {
return this;
} else {
this.denominator = this.denominator / gcd;
this.numerator = this.numerator / gcd;
return this;
}
}
/**
* 加法
*
* @param fraction
* @return
*/
public Fraction add(Fraction fraction) {
Integer resultDenominator = Math.abs(this.denominator)
* Math.abs(fraction.getDenominator());
Integer resultNumerator = this.numerator * fraction.getDenominator()
+ this.denominator * fraction.getNumerator();
Fraction result = new Fraction(resultNumerator, resultDenominator);
return result.simplify();
}
/**
* 减法
*
* @param fraction
* @return
*/
public Fraction minus(Fraction fraction) {
Integer resultDenominator = Math.abs(this.denominator)
* Math.abs(fraction.getDenominator());
Integer resultNumerator = this.numerator * fraction.getDenominator()
- this.denominator * fraction.getNumerator();
Fraction result = new Fraction(resultNumerator, resultDenominator);
return result.simplify();
}
/**
* 乘法
*
* @param fraction
* @return
*/
public Fraction multiply(Fraction fraction) {
Fraction result = null;
if (this.numerator == 0 || fraction.numerator == 0) {
result = new Fraction(0, null);
}
Integer resultDenominator = this.denominator
* fraction.getDenominator();
Integer resultNumerator = this.numerator * fraction.getNumerator();
result = new Fraction(resultNumerator, resultDenominator);
return result.simplify();
}
/**
*除法
*
* @param fraction
* @return
*/
public Fraction divide(Fraction fraction) {
Fraction result = null;
if (this.numerator == 0) {
result = new Fraction(0, null);
}
Integer resultDenominator = this.denominator * fraction.getNumerator();
Integer resultNumerator = this.numerator * fraction.getDenominator();
result = new Fraction(resultNumerator, resultDenominator);
return result.simplify();
}
@Override
public String toString() {
return "[分子]" + this.numerator + "-[分母]" + this.denominator;
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
Fraction f1 = new Fraction(1, 2);
Fraction f2 = new Fraction(2, 4);
System.out.println(f1);
System.out.println(f2);
System.out.println(f2.simplify());
System.out.println(f1.add(f2));
System.out.println(f1.minus(f2));
System.out.println(f1.multiply(f2));
System.out.println(f1.divide(f2));
}
}
Java代码写一个分数排序,同分同名
ListInteger list = new ArrayList();
list.add(100);
list.add(100);
list.add(92);
list.add(90);
list.add(90);
list.add(80);
list.add(10);
for (Integer i : list) {
long count = list.stream().filter(integer - integer i).count() + 1;
System.out.println("分数:" + i + " 名次:" + count);
}
用Java语言编写一个代码,输入几个评委分数,去掉一个最大和最小取平均值,
package com.test;
import java.io.InputStream;
import java.util.Scanner;
public class Test {
int[] nums = new int[5]; //评委分数数组
int avg; //平均分
int num=0; //输入的值
int num2=0; //冒泡用的
int num3=0; //冒泡用的
int sun=0; //总分
Scanner input = new Scanner(System.in);
public void test(){
for(int i=0;i5;i++){ // 输入5个评委分
System.out.println("请评委输入分数:");
num = input.nextInt();
if(num0 num=10){
nums[i]=num;
}else{
System.out.println("分数只能在0-10之间");
i--;
continue;
}
}
for(int i=0;inums.length;i++){
sun=sun+nums[i];
}
for(int i = 0; inums.length-1 ;i++){
if(nums[i] nums[i+1]){
num2 = nums[i];
nums[i]=nums[i+1];
nums[i+1]=num2;
}
}
for(int i = 1; inums.length-1 ;i++){
if(nums[i] nums[i+1]){
num3 = nums[i];
nums[i]=nums[i+1];
nums[i+1]=num2;
}
}
avg=(sun-nums[0]-nums[1])/3;
System.out.println("平均分为:"+avg);
}
public static void main(String[] args) {
Test tt = new Test();
tt.test();
}
}
本文题目:java分数代码 java学生成绩代码
标题链接:http://pwwzsj.com/article/hgisho.html