思路: 排序肯定還是要排的, 按照J(rèn)ava成績來進行排練. 然后排名的時候,進行比較. 如果這一名的成績和上一名的相同, 那么名次相同, 如果比上一名分?jǐn)?shù)低,那么排名加一.

成都創(chuàng)新互聯(lián)公司是專業(yè)的孟連網(wǎng)站建設(shè)公司,孟連接單;提供成都網(wǎng)站設(shè)計、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行孟連網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
可以使用傳統(tǒng)的,集合排序,輸出. 也可以使用java8新提供的Stream API進行操作
參考代碼如下
import?java.util.*;
import?java.util.Map.Entry;
import?java.util.stream.Collectors;
class?Stu?{//?學(xué)生類
private?String?name;
private?double?score;//?成績
public?Stu(String?name,?double?score)?{
this.name?=?name;
this.score?=?score;
}
public?double?getScore()?{
return?score;
}
public?void?setScore(double?score)?{
this.score?=?score;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
}
//測試類
public?class?TestDemo?{
public?static?void?main(String[]?args)?{
ListStu?stus?=?Arrays.asList(new?Stu("Tom",?79.5),?new?Stu("Jack",?52),?new?Stu("Amdy",?79.5),
new?Stu("Lucy",?68),?new?Stu("Cherry",?79.5),?new?Stu("Jerry",?52),?new?Stu("Sweet",?91),
new?Stu("Solem",?65));
fun1(stus);
System.out.println("---------------分割線---------------------");
fun2(stus);
}
//?方法一:傳統(tǒng)的方法
public?static?void?fun1(ListStu?stus)?{
//?按照成績排序
stus.sort(new?ComparatorStu()?{
@Override
public?int?compare(Stu?s1,?Stu?s2)?{
return?-Double.compare(s1.getScore(),?s2.getScore());
}
});
int?index?=?0;//?排名
double?lastScore?=?-1;//?最近一次的分
for?(int?i?=?0;?i??stus.size();?i++)?{
Stu?s?=?stus.get(i);
if?(Double.compare(lastScore,?s.getScore())!=0)?{?//?如果成績和上一名的成績不相同,那么排名+1
lastScore?=?s.getScore();
index++;
}
System.out.println("名次:"?+?index?+?"\t分?jǐn)?shù)"?+?s.getScore()?+?"\t名字"?+?s.getName());
}
}
//?方法2:?Java8開始支持的Lambada表達式配合?Stream?API?來進行分組排序
public?static?void?fun2(ListStu?stus)?{
ListEntryDouble,?ListStu?list?=?stus.stream().collect(Collectors.groupingBy(Stu::getScore)).entrySet()
.stream().sorted((s1,?s2)?-?-Double.compare(s1.getKey(),?s2.getKey())).collect(Collectors.toList());
int?index?=?1;
for?(EntryDouble,?ListStu?entry?:?list)?{
System.out.print("名次:"?+?index?+?"\t分?jǐn)?shù):"?+?entry.getKey()?+?"\t名字");
entry.getValue().forEach((s)?-?System.out.print("??"?+?s.getName()));
System.out.println();
index++;
}
}
}
輸出結(jié)果
名次:1 分?jǐn)?shù)91.0 名字Sweet
名次:2 分?jǐn)?shù)79.5 名字Tom
名次:2 分?jǐn)?shù)79.5 名字Amdy
名次:2 分?jǐn)?shù)79.5 名字Cherry
名次:3 分?jǐn)?shù)68.0 名字Lucy
名次:4 分?jǐn)?shù)65.0 名字Solem
名次:5 分?jǐn)?shù)52.0 名字Jack
名次:5 分?jǐn)?shù)52.0 名字Jerry
名次:1 分?jǐn)?shù):91.0 名字??Sweet
名次:2 分?jǐn)?shù):79.5 名字??Tom??Amdy??Cherry
名次:3 分?jǐn)?shù):68.0 名字??Lucy
名次:4 分?jǐn)?shù):65.0 名字??Solem
名次:5 分?jǐn)?shù):52.0 名字??Jack??Jerry
---------------分割線---------------------
名次:1 分?jǐn)?shù):91.0 名字??Sweet
名次:2 分?jǐn)?shù):79.5 名字??Tom??Amdy??Cherry
名次:3 分?jǐn)?shù):68.0 名字??Lucy
名次:4 分?jǐn)?shù):65.0 名字??Solem
名次:5 分?jǐn)?shù):52.0 名字??Jack??Jerry
package?image;
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?{
String[][]?list;????//?學(xué)生數(shù)組
int?count?=?0;????????//?學(xué)生人數(shù)
int?field?=?2;????????//?屬性個數(shù)
Scanner?sc?=?new?Scanner(System.in);
System.out.println("輸入學(xué)生個數(shù):");
count?=?sc.nextInt();
list?=?new?String[count][field];
for?(int?i?=?0;?i??count;?i++)?{
System.out.println("輸入第"+(i+1)+"個學(xué)生姓名和分?jǐn)?shù):");????
System.out.println("姓名:");
String?name?=?sc.next();
System.out.println("分?jǐn)?shù):");
String?score?=?sc.next();
list[i][0]?=?name;
list[i][1]?=?score;
}
//?獲取最高分
double?maxScore?=?0;
for?(int?i?=?0;?i??count;?i++)?{
double?score?=?Double.parseDouble(list[i][1]);
if?(maxScore??score)?{
maxScore?=?score;
}
}
//?獲取最高分同學(xué)
String?maxScoreName?=?null;
for?(int?i?=?0;?i??count;?i++)?{
String?name?=?list[i][0];
double?score?=?Double.parseDouble(list[i][1]);
if?(score?==?maxScore)?{
if?(maxScoreName?==?null)?{
maxScoreName?=?name;
}else{
maxScoreName?=?maxScoreName?+?","?+?name;????????????????????
}
}
}
System.out.println("最高分:"?+?maxScore);
System.out.println("成績最高同學(xué)姓名:"?+?maxScoreName);
sc.close();
}
}
結(jié)果:
輸入學(xué)生個數(shù):
3
輸入第1個學(xué)生姓名和分?jǐn)?shù):
姓名:
fanhongwei
分?jǐn)?shù):
90
輸入第2個學(xué)生姓名和分?jǐn)?shù):
姓名:
chen
分?jǐn)?shù):
89
輸入第3個學(xué)生姓名和分?jǐn)?shù):
姓名:
maozedong
分?jǐn)?shù):
90
最高分:90.0
成績最高同學(xué)姓名:fanhongwei,maozedong
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名學(xué)生的成績:");
Scanner input = new Scanner(System.in);
while(count5){
try{
if(count 5){
System.out.println("請輸入第"+(count+1)+"個分?jǐn)?shù):");
}
inputStr=input.nextLine();
scores[count++]=Double.valueOf(inputStr.trim());
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim())){
System.out.println("您已成功結(jié)束程序");
System.exit(0);
}
System.out.println("若想結(jié)束請輸入:exit");
System.out.print("您輸入的分?jǐn)?shù)不是數(shù)值類型,");
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("請輸入要統(tǒng)計學(xué)生的成績(理論上可以輸入無限個,前提是你有那么大的內(nèi)存):");
while(true){
try{
System.out.println("請輸入第"+count+++"個分?jǐn)?shù),或輸入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("您已成功結(jié)束程序");
input.close();
System.exit(0);
}
if(inputStr!=null "ok".equals(inputStr.trim().toLowerCase())){
break;
}
System.out.println("您輸入的分?jǐn)?shù)不是數(shù)值類型,");
System.out.println("若想結(jié)束請輸入exit ,若想計算結(jié)果請輸入ok");
count--;
}
}
if(inputScores.size()==0){
System.out.println("您沒有輸入學(xué)生成績,無數(shù)據(jù)可統(tǒng)計,程序結(jié)束。");
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);
}
}
1、可以使用lambda來實現(xiàn)
int[] ints = new Random().ints(1, 100).distinct().limit(20).sorted().toArray();
//--統(tǒng)計信息
LongSummaryStatistics stats = Arrays.stream(ints)
.mapToLong((x) - x)
.summaryStatistics();
System.out.println(stats);
//--輸出
IntStream.range(0, 5)
.forEach(i - System.out.println(ints[i]));//前五
IntStream.range(0, ints.length)
.forEach(i - System.out.println(ints[i]));//所有
//
import?java.util.Scanner;
//
public?class?Test2014?{
public?static?void?main(String[]?args)?{
Scanner?sc?=?new?Scanner(System.in);
System.out.println("輸入學(xué)生人數(shù):");
int?n?=?sc.nextInt();
int?sum?=?0;
for(int?i?=?1;i?=?n;++i){
System.out.println("輸入第"+i+"個學(xué)生成績:");
sum?+=?sc.nextInt();
}
System.out.println("總成績是:"+sum+"?"+"平均成績是:"+(double)sum/n);
}
}
import?java.util.Scanner;
public?class?Maxgreat?
{
public?static?void?main(String?args[])
{
Scanner?sc=new?Scanner(System.in);
int?great[]=new?int[5];
int?max;
System.out.println("請輸入五人成績:");
for(int?i=0;i5;i++)
{
great[i]=sc.nextInt();
}
max=great[0];
for(int?j=1;j5;j++)
{
if(maxgreat[j])
{
max=great[j];
}
}
System.out.println("成績最高為:"+max);
}
}
名稱欄目:java成績前五名的代碼 java成績等級
當(dāng)前路徑:http://chinadenli.net/article34/dodjjpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、App開發(fā)、微信公眾號、搜索引擎優(yōu)化、App設(shè)計、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)