求大神指導(dǎo)下哪里出錯(cuò)了,為什么運(yùn)行有問題
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("考試成績(jī)的前三名為");
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]);
}
}
} ? ? ?
運(yùn)行結(jié)果:
考試成績(jī)的前三名為
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at HelloWorld.show(HelloWorld.java:15)
at HelloWorld.main(HelloWorld.java:9)
2016-05-30
循環(huán)從nums.length-1開始
2016-05-30
for(int i=nums.length;i>=0;i--)數(shù)組越界了。應(yīng)該為:for(int i=nums.length-1;i>=0;i--)
2016-05-30
package com.imooc;
import java.util.Scanner;
/*
?* 功能:為指定的成績(jī)加分,直到分?jǐn)?shù)大于等于60為止
?* 輸出加分前的成績(jī)和加分后的成績(jī),并且統(tǒng)計(jì)加分的次數(shù)
?* 步驟:
?* 1.定義一個(gè)變量,用來統(tǒng)計(jì)加分的次數(shù)
?* 2.使用循環(huán)為成績(jī)加分
?* 3.每次執(zhí)行循環(huán)時(shí)加1分,并且統(tǒng)計(jì)加分的次數(shù)
?*?
?* 使用Scanner工具類來獲取用戶輸入的值
?* Scanner類位于java.util包中,使用時(shí)需要導(dǎo)入此包
?* 步驟:
?* 1.導(dǎo)入java.util.Scanner
?* 2.創(chuàng)建Scanner對(duì)象
?* 3.接收并保存用戶輸入的值
?*/
public class Demo01 {
public static void main(String[] args){
Scanner input=new Scanner(System.in); //創(chuàng)建Scanner對(duì)象
//print和println區(qū)別:println輸出信息后會(huì)換行,而print不會(huì)換行
System.out.println("請(qǐng)輸入您的考試成績(jī):");
int score=input.nextInt(); //獲取成績(jī)信息并保存在變量score中
int count=0; ?//統(tǒng)計(jì)次數(shù)
System.out.println("加分前的成績(jī):"+score);
while(score<60){
score++;//每次循環(huán)加1分
count++;//統(tǒng)計(jì)加分的次數(shù)
}
System.out.println("加分后的成績(jī):"+score);
System.out.println("共加了"+count+"次!");
}
}