求大神指導下哪里出錯了,為什么運行有問題
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? HelloWorld hello=new HelloWorld();
? ? int[] nums={89 , -23 , 64 , 91 , 119 , 52 , 73};
System.out.println("考試成績的前三名為");
hello.show(nums);
}
public void show(int[] nums){
Arrays.sort(nums);
int sum=0;
for(int i=nums.length;i>=0;i--){
if(nums[i]<0||nums[i]>100){
continue;
}
sum++;
if(sum>3){
break;
}
System.out.println(nums[i]);
}
}
} ? ? ?
運行結果:
考試成績的前三名為
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at HelloWorld.show(HelloWorld.java:15)
at HelloWorld.main(HelloWorld.java:9)
2016-05-30
循環從nums.length-1開始
2016-05-30
for(int i=nums.length;i>=0;i--)數組越界了。應該為:for(int i=nums.length-1;i>=0;i--)
2016-05-30
package com.imooc;
import java.util.Scanner;
/*
?* 功能:為指定的成績加分,直到分數大于等于60為止
?* 輸出加分前的成績和加分后的成績,并且統計加分的次數
?* 步驟:
?* 1.定義一個變量,用來統計加分的次數
?* 2.使用循環為成績加分
?* 3.每次執行循環時加1分,并且統計加分的次數
?*?
?* 使用Scanner工具類來獲取用戶輸入的值
?* Scanner類位于java.util包中,使用時需要導入此包
?* 步驟:
?* 1.導入java.util.Scanner
?* 2.創建Scanner對象
?* 3.接收并保存用戶輸入的值
?*/
public class Demo01 {
public static void main(String[] args){
Scanner input=new Scanner(System.in); //創建Scanner對象
//print和println區別:println輸出信息后會換行,而print不會換行
System.out.println("請輸入您的考試成績:");
int score=input.nextInt(); //獲取成績信息并保存在變量score中
int count=0; ?//統計次數
System.out.println("加分前的成績:"+score);
while(score<60){
score++;//每次循環加1分
count++;//統計加分的次數
}
System.out.println("加分后的成績:"+score);
System.out.println("共加了"+count+"次!");
}
}