chen7841835
2015-03-26 16:38
考试成绩的前三名为: -23 52 64
import java.util.Arrays; public class HelloWorld { public static void main(String[] args) { HelloWorld hello = new HelloWorld(); int[] scores ={89,-23,64,91,119,52,73}; Arrays.sort(scores); System.out.println("考试成绩的前三名为:"); hello.getScores(scores); } public void getScores(int[] scores){ int count = 0; for(int i=scores.length-1;i>=0;i--){ if(scores[i]<=100 && scores[i] >=0 && count < 3){ System.out.println(scores[i]); count += 1; } } } }
为什么楼主要自己定义方法排序呢?有sort方法啊。会简洁不少。
a=3;
并不是a就是3,而是a指向3
所以
int a,b;
a=a+b;
b=a-b;
a=a-b;
这样可以交换这两个数...
package com.rui.text;
public class JavaDemoTest {
public static void main(String[] args) {
JavaDemoTest hello=new JavaDemoTest();
int []scores={89,-23,64,91,119,52,73};
hello.get(scores);
}
public void get(int scores[]){
for (int i = 0; i < scores.length; i++) {
if (scores[i]>100 || scores[i]<0) scores[i]=0;
}
for (int i = 0; i < scores.length; i++) {
for (int j = 0; j < scores.length-1-i; j++) {
if(scores[j]<scores[j+1]){
//交换位置
scores[j]=scores[j]+scores[j+1];
scores[j+1]=scores[j]-scores[j+1];
scores[j]=scores[j]-scores[j+1];
}
}
}
System.out.println("");
//System.out.println(scores[t-1]+"\n"+scores[t-2]+"\n"+scores[t-3]);
System.out.println(scores[0]+"\n"+scores[1]+"\n"+scores[2]);
}
}
你都没判断 ,
Java入门第一季(IDEA工具)升级版
1165172 学习 · 17581 问题
相似问题