import?java.util.Random;
網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)專注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營(yíng)網(wǎng)站定制開(kāi)發(fā).小程序定制開(kāi)發(fā),H5頁(yè)面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為混凝土攪拌機(jī)等企業(yè)提供專業(yè)服務(wù)。
public?class?RandomDemo?{
public?static?void?main(String[]?args)?{
Random?r?=?new?Random();
float?random?=?r.nextFloat();
float?random2?=?random?*?100;
String?grade?=?String.format("%.1f",random2);
//System.out.println(grade);
float?grade2?=?Float.parseFloat(grade);
if(grade2?=?90){
System.out.println("A");
}else?if(grade2?=?60??grade2??90){
System.out.println("B");
}else{
System.out.println("C");
}
}
}
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("請(qǐng)輸入5名學(xué)生的成績(jī):");
Scanner input = new Scanner(System.in);
while(count5){
try{
if(count 5){
System.out.println("請(qǐng)輸入第"+(count+1)+"個(gè)分?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é)束請(qǐng)輸入: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("總成績(jī)是" + 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("請(qǐng)輸入要統(tǒng)計(jì)學(xué)生的成績(jī)(理論上可以輸入無(wú)限個(gè),前提是你有那么大的內(nèi)存):");
while(true){
try{
System.out.println("請(qǐng)輸入第"+count+++"個(gè)分?jǐn)?shù),或輸入ok進(jìn)行計(jì)算,離開(kāi)請(qǐng)輸入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é)束請(qǐng)輸入exit ,若想計(jì)算結(jié)果請(qǐng)輸入ok");
count--;
}
}
if(inputScores.size()==0){
System.out.println("您沒(méi)有輸入學(xué)生成績(jī),無(wú)數(shù)據(jù)可統(tǒng)計(jì),程序結(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("總成績(jī)是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
}
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.println("請(qǐng)輸入成績(jī)");
int a=x.nextInt();
int n=a/10; //百分制轉(zhuǎn)換為等第
switch(n){
case 10: //表示如果n=10,也就是一百分,輸出A
case 9:
System.out.println("A");
break;
case 8:
System.out.println("B");
break;
case 7:
System.out.println("C");
break;
case 6:
System.out.println("D");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
System.out.println("FAIL"); //低于六十分的輸出不及格 FAIL
break;
default:System.out.println(" 請(qǐng)輸入一個(gè)正確的成績(jī)"); //輸入的不是百分制,報(bào)錯(cuò)
}
}
}
思路: 排序肯定還是要排的, 按照J(rèn)ava成績(jī)來(lái)進(jìn)行排練. 然后排名的時(shí)候,進(jìn)行比較. 如果這一名的成績(jī)和上一名的相同, 那么名次相同, 如果比上一名分?jǐn)?shù)低,那么排名加一.
可以使用傳統(tǒng)的,集合排序,輸出. 也可以使用java8新提供的Stream API進(jìn)行操作
參考代碼如下
import?java.util.*;
import?java.util.Map.Entry;
import?java.util.stream.Collectors;
class?Stu?{//?學(xué)生類
private?String?name;
private?double?score;//?成績(jī)
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;
}
}
//測(cè)試類
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)?{
//?按照成績(jī)排序
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)?{?//?如果成績(jī)和上一名的成績(jī)不相同,那么排名+1
lastScore?=?s.getScore();
index++;
}
System.out.println("名次:"?+?index?+?"\t分?jǐn)?shù)"?+?s.getScore()?+?"\t名字"?+?s.getName());
}
}
//?方法2:?Java8開(kāi)始支持的Lambada表達(dá)式配合?Stream?API?來(lái)進(jìn)行分組排序
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
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入學(xué)生的分?jǐn)?shù):");
double score = input.nextDouble();
int level1,level2;
if(score=60.0 score=100.0){
System.out.print("該學(xué)生成績(jī)?yōu)閮?yōu)秀!");
}
else if(score=0 score60.0){
System.out.print("該學(xué)生成績(jī)?yōu)椴患案瘢?);
}
else{
System.out.print("您輸入的成績(jī)有誤!");
}
}?? ?
}
將成績(jī)分為幾個(gè)等級(jí),且每個(gè)等級(jí)都有一個(gè)標(biāo)記符,然后使用switch case來(lái)匹配標(biāo)記符分別來(lái)判斷,即可實(shí)現(xiàn)。
示例:
switch(表達(dá)式){
case 常量表達(dá)式1:
//執(zhí)行語(yǔ)句1;
break;
case 常量表達(dá)式2:
//執(zhí)行語(yǔ)句2;
break;
default:
//執(zhí)行語(yǔ)句;
break;
}
1.default就是如果沒(méi)有符合的case就執(zhí)行它,default并不是必須的.
2.case后的語(yǔ)句可以不用大括號(hào).
3.switch語(yǔ)句的判斷條件可以接受int,byte,char,short,枚舉,不能接受其他類型.
4.一旦case匹配,就會(huì)順序執(zhí)行后面的程序代碼,而不管后面的case是否匹配,直到遇見(jiàn)break,利用這一特性可以讓好幾個(gè)case執(zhí)行統(tǒng)一語(yǔ)句.
5.切記不要忘記寫(xiě)break;
新聞標(biāo)題:java成績(jī)分段代碼 java編寫(xiě)成績(jī)等級(jí)劃分
分享路徑:http://aaarwkj.com/article16/hhpjgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、微信小程序、面包屑導(dǎo)航、網(wǎng)站改版、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)