Num++,出現(xiàn)了什么問題?
import java.util.Arrays;
public class HelloWorld {
?public static void main(String[] args) {
? ? ? ? int scores[]= {89,-23,64,91,119,52,73};
? ? ? ?System.out.print("前三名是:");
? ? ? ?HelloWorld hello=new HelloWorld();
? ? ? ?hello.max(scores);
? ? }
? ? ? ?public void max(int scores[]){
? ? ? ? ?int Num=0;
? ? ? ? ? ? ?for(int i=scores.length;i>0&&Num<=3;i--)
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? if(scores[i]<0||scores[i]>100)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? Num ++;
? ? ? ? ? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ?}
? ? ? ?}
}
2019-02-20
你的索引值超出了數(shù)組范圍,應(yīng)改為scores.length-1;
continue執(zhí)行后,直接進(jìn)入下一個循環(huán),下面的代碼不會執(zhí)行。
2019-02-20
import java.util.Arrays;
public class HelloWorld {
?public static void main(String[] args) {
? ? ? ? int scores[]= {89,-23,64,91,119,52,73};
? ? ? ?System.out.print("前三名是:");
? ? ? ?HelloWorld hello=new HelloWorld();
? ? ? ?hello.max(scores);
? ? }
? ? ? ?public void max(int scores[]){
? ? ? ? ?int Num=0;
? ? ? ? ? ? ?for(int i=scores.length-1;i>0&&Num<=3;i--)
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? if(scores[i]<0||scores[i]>100)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? }
Num++;
System.out.println(scores[i]);
? ? ? ? ? ? ?}
? ? ? ?}
}