50,504
社区成员
发帖
与我相关
我的任务
分享
import java.util.Arrays;
import java.util.Scanner;
public class WhoAreYou {
public static void main(String[] args) {
WhoAreYou hello = new WhoAreYou();
int[] scores = {89,-23,64,91,119,52,73};
hello.showTop3(scores);
System.out.println("前三名成绩为:");
}
public void showTop3(int[]scores){
int j = 0;
int[]score = {0};
for(int i = 0; i < scores.length ; i++){
if (scores[i]<100 && scores[i]>0){
score[j]=scores[i];
j++;
}if (scores[i]>100 || scores[i]<0){
continue;
}
}
Arrays.sort(score);
for(int s =1 ; s < 3 ; s++ ){
System.out.println(score[-s]);
}
}
}
public static void main(String[] args) {
Test hello = new Test();
int[] scores = { 89, -23, 64, 91, 119, 52, 73 };
System.out.println("前三名成绩为:");
hello.showTop3(scores);
}
public void showTop3(int[] scores) {
int j = 0;
int[] score = new int[scores.length]; //长度
for (int i = 0; i < scores.length; i++) {
if (scores[i] < 100 && scores[i] > 0) {
score[j] = scores[i];
j++;
}
if (scores[i] > 100 || scores[i] < 0) {
continue;
}
}
Arrays.sort(score);
for (int s = scores.length-1; s >= scores.length - 3; s--) {
System.out.println(score[s]);
}
}
for(int s =3 ; s > 0 ; s-- ){
System.out.println(score[s-1]);
}
public class test2 {
public static void main(String[] args) {
test2 hello = new test2();
int[] scores = { 89, -23, 64, 91, 119, 52, 73 };
hello.showTop3(scores);
System.out.println("前三名成绩为:");
}
public void showTop3(int[] scores) {
int j = 0;
int[] score = new int[scores.length];
for (int i = 0; i < scores.length; i++) {
if (scores[i] < 100 && scores[i] > 0) {
score[j] = scores[i];
j++;
}
if (scores[i] > 100 || scores[i] < 0) {
continue;
}
}
Arrays.sort(score);
for (int s = score.length - 1; s > score.length - 4; s--) {
System.out.println(score[s]);
}
}
}
慢慢研究package array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 打印前三名学生成绩
* @author cloud
*
*/
public class ArraySort {
public static void main(String[] args) {
// 成绩数组
int[] scores = {89, -23, 64, 91, 119, 52, 73};
// 成绩集合
List<Integer> scoreList = new ArrayList<Integer>();
// 将数据保存到集合
for (int i = 0; i < scores.length; i++) {
// 取成绩
int score = scores[i];
// 判断是否为正常值
if(score <= 100 && score >= 0){
scoreList.add(scores[i]);
}
}
// 对数组数据进行排序
Collections.sort(scoreList);
// 得到集合长度
int size = scoreList.size();
// 将前三名学生成绩放入集合
scoreList = scoreList.subList(size - 3, size);
// 打印前三名成绩
for (Integer score : scoreList) {
System.out.println(score);
}
}
}