求大神解答,這個(gè)錯(cuò)在哪里了
public static void main(String[] args) {
// TODO Auto-generated method stub
//創(chuàng)建一個(gè)學(xué)生成績(jī)數(shù)組:
int[] scores = {77,96,35,48,79,98,65,77};
Students stu = new Students();
stu.show(scores);
}
//先創(chuàng)建一個(gè)方法,用來(lái)保學(xué)生成績(jī);因?yàn)樾枰獋魅牍潭ǔ煽?jī),需要運(yùn)用數(shù)組;
public void show(int [] scores){
//定義一個(gè)變量來(lái)保存學(xué)生成績(jī)
int a = 0;
Arrays.sort(scores);
for(int i = scores.length; i >= 0; i --){
if(scores[i]<0 || scores[i] > 100){
System.out.println("您輸入的成績(jī)有誤,請(qǐng)重新輸入!");
continue;
}else{
a++;
}
if(a>3){
break;
}
System.out.println("前三名的學(xué)生成績(jī)是:"+ scores[i]);
}?
2017-06-12
package com.hwadee.day612;
import java.util.Arrays;//首先你沒(méi)有導(dǎo)入數(shù)組包,你在數(shù)組中用到了數(shù)組的排序你就需要導(dǎo)入數(shù)組包
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//創(chuàng)建一個(gè)學(xué)生成績(jī)數(shù)組:
int[] scores = {77,96,35,48,79,98,65,77};
Students stu = new Students();//你這個(gè)創(chuàng)建了一個(gè)Students對(duì)象,但是你的Students類(lèi)不純?cè)凇?/p>
stu.show(scores);//這是你對(duì)象的成員方法,但是你并沒(méi)有定義在Students類(lèi)里面。
}
}
class Students{
//先創(chuàng)建一個(gè)方法,用來(lái)保學(xué)生成績(jī);因?yàn)樾枰獋魅牍潭ǔ煽?jī),需要運(yùn)用數(shù)組;
public void show(int [] scores){
//定義一個(gè)變量來(lái)保存學(xué)生成績(jī)
int a = 0;
Arrays.sort(scores);
for(int i = scores.length - 1; i >= 0; i --){//數(shù)組下標(biāo)是從0開(kāi)始的,所以起始位置應(yīng)該是scores.length-1,而不是scores.length不然會(huì)報(bào)數(shù)組下標(biāo)越界錯(cuò)誤
if(scores[i]<0 || scores[i] > 100){
System.out.println("您輸入的成績(jī)有誤,請(qǐng)重新輸入!");
continue;
}else{
a++;
}
if(a>3){
break;
}
System.out.println("前三名的學(xué)生成績(jī)是:"+ scores[i]);
}
}
}
在上面就是你的所有問(wèn)題所在,望采納。
2017-06-12
Arrays.sort不能用,沒(méi)有加impot java.util.Arrays