小白问一个基础的问题,希望各位老手解答一下

cz_qs 2017-03-02 11:54:07
初学java,遇到一个问题 ,题目要求为:
1、 考试成绩已保存在数组 scores 中,数组元素依次为 89 , -23 , 64 , 91 , 119 , 52 , 73
2、 要求通过自定义方法来实现成绩排名并输出操作,将成绩数组作为参数传入
3、 要求判断成绩的有效性( 0—100 ),如果成绩无效,则忽略此成绩

我的代码如下

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]);
}
}
}


为什么无法输出,是哪里出现了问题需要更改,希望各位大神可以为我解答一下
...全文
149 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
菜 头 2017-03-03
  • 打赏
  • 举报
回复
还有你要输出排名前三名的,你应该在存放的是否做下冒泡排序吧
菜 头 2017-03-03
  • 打赏
  • 举报
回复
s应该从0开始吧,还有score下标怎么是-s呢,应该是s吧
自由自在_Yu 2017-03-03
  • 打赏
  • 举报
回复
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]);
		}
	}
max_min_ 2017-03-03
  • 打赏
  • 举报
回复
 for(int s =3 ; s > 0 ; s-- ){
            System.out.println(score[s-1]);
            }
自由自在_Yu 2017-03-03
  • 打赏
  • 举报
回复
引用 1 楼 qnmdcsdn 的回复:

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]);
		}
	}
}
慢慢研究
  • 打赏
  • 举报
回复

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]);
		}
	}
}
慢慢研究
Java_攻城狮 2017-03-03
  • 打赏
  • 举报
回复
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);
		}
	}
}
Java_攻城狮 2017-03-03
  • 打赏
  • 举报
回复
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); } } }
qq_37744599 2017-03-03
  • 打赏
  • 举报
回复
hello.showTop3(scores);这个是什么意思啊?
cz_qs 2017-03-03
  • 打赏
  • 举报
回复
引用 1 楼 qnmdcsdn 的回复:

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]);
		}
	}
}
慢慢研究
完美解决,感谢大神
cz_qs 2017-03-03
  • 打赏
  • 举报
回复
引用 6 楼 u011320740 的回复:
还有你要输出排名前三名的,你应该在存放的是否做下冒泡排序吧
还没学到冒泡那里

50,504

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧