package chapter;
import java.util.Arrays;
/**
* Created by 钊哥 on 2017/5/14.
*/
public class nineteen {
public static void main(String args[]){
int []scores= new int[]{89,-23,64,91,119,52,73};
nineteen zhao=new nineteen();
zhao.xin(scores);
}
public void xin(int[]scores){
Arrays.sort(scores);
int nums=0;
for(int i=scores.length;i>=0;i--) {
if (nums > 3) {
System.out.println("考试成绩前"+nums+"名为:");
break;
}
if (scores[i] >= 100 || scores[i] <= 0) {
continue;
} else {
nums++;
System.out.println(scores[i]);
}
}
}
}
不是没有导入包的问题,是数组越界。
package exercise;
import java.util.Arrays;
public class Nineteen {
public static void main(String args[]){
int []scores= new int[]{89,-23,64,91,119,52,73};
Nineteen zhao=new Nineteen();
zhao.xin(scores);
}
public void xin(int[]scores){
Arrays.sort(scores);
int nums=0;
for(int i=scores.length-1;i>=0;i--) {
if (nums >= 3) {
System.out.println("考试成绩前"+nums+"名为:");
break;
}
if (scores[i] >= 100 || scores[i] <= 0) {
continue;
} else {
nums++;
System.out.println(scores[i]);
}
}
}
}
给你改正了两处
有啊
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at chapter.nineteen.xin(nineteen.java:20)
at chapter.nineteen.main(nineteen.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
运行的时候弹出这个
import java.util.Arrays;
你没有导入数组包