請(qǐng)問怎么沒辦法輸出前三名,是方法哪里寫錯(cuò)了
package grade;
import java.util.Arrays;
/*1、 考試成績(jī)已保存在數(shù)組 scores 中,數(shù)組元素依次為 89 , -23 , 64 , 91 , 119 , 52 , 73
2、 要求通過自定義方法來實(shí)現(xiàn)成績(jī)排名并輸出操作,將成績(jī)數(shù)組作為參數(shù)傳入
3、 要求判斷成績(jī)的有效性( 0—100 ),如果成績(jī)無效,則忽略此成績(jī)*/
public class one{
public void grade(int []grades) {
Arrays.sort(grades);
int[] G = new int[3];
int j = 0,q = 0;
for(int i =grades.length-1; i>=0 ; i--) {
if(grades[i] >0 && grades[i] < 100) {
? ?G[q] = grades[i];
? q++;
? j++;
}
?
if(j > 3) break;
}
for(q=0;q<G.length;q++) {
System.out.println(G[q]);
}
}
}
2019-04-13
import java.util.Arrays;
public class HelloWorld{
? ? public static void main(String[] args){
? ? ? ? int[] scores = new int[]{89,-23,64,91,119,52,73};
? ? ? ? System.out.println("考試成績(jī)的前三名為:");
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? hello.showTop3(scores);? ??
? ? }
? ?
? ??
? ? public void showTop3(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int num=0;
? ? ? ? for(int i=(scores.length-1);i>=0;i--){
? ? ? ? ? ? if(scores[i]>=0 && scores[i]<=100){
? ? ? ? ? ? ? ? num++;
? ? ? ? ? ? ? ? if(num<=3){
? ? ? ? ? ? ? ? ? ? System.out.println(scores[i]);? ??
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }? ??
? ? }
}
2019-04-05
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ?int[] scores={89,-23,64,91,119,52,73};? ?
? ? ?HelloWorld hello = new HelloWorld();
? ? ?hello.sortAndPrint(scores);
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ? public void sortAndPrint(int[] xscores){
? ? ? ? Arrays.sort(xscores);//排序,但默認(rèn)是升序
? ? ? ? int len = xscores.length;
? ? ? ? int[] scores= new int[len];
? ? ? ? int count =0;//計(jì)數(shù)
? ? ? ? //把升序變?yōu)榻敌?/p>
? ? ? ? for(int i=0;i<xscores.length;i++){
? ? ? ? ? ? scores[i]=xscores[len-1-i];
? ? ? ? }
? ? ? ? xscores=scores;
? ? ? ? //輸出前三
? ? ? ? for(int i=0;i<xscores.length;i++){
? ? ? ? ? ? //驗(yàn)證數(shù)據(jù)是否有效
? ? ? ? ? ? if(xscores[i]>0&&xscores[i]<100){
? ? ? ? ? ? System.out.println(xscores[i]);
? ? ? ? ? ? //找到即輸出,計(jì)數(shù)+1
? ? ? ? ? ? count++;
? ? ? ? ? ? //找到三個(gè),跳出循環(huán)
? ? ? ? ? ? if(count==3)
? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
這是我做的,可用,你可以對(duì)比一下
2019-04-05
2019-03-26
太多錯(cuò)了,主函數(shù)都沒有,上面的代碼方法也沒,你調(diào)用的是其他頁面的方法?