這個(gè)程序可以正確執(zhí)行嗎?
import java.util.Arrays;
import java.util.Scanner;
public class HelloWorld {
? ??
? ? public static void main(String[] args) {
? ? ? ? System.out.println("請輸入考試成績:");
?
? ? ? ?//定義一個(gè)數(shù)組,并從鍵盤輸入數(shù)據(jù)。
? ? ? ?int[] scores;
? ? ? ? for (int i=1;i<=9;i++){
? ? ? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? ? ? int nums= input.nextInt(); ?
? ? ? ? ? ? scores[i] = nums;
? ? ? ? }
? ? ? ? System.out.println("考試前三名成績?yōu)椋?);
? ? ? ? HelloWorld topthree=new HelloWorld(); ? ?
? ? ? ? topthree.three(scores);
? ? }
? ??
? ? //百分試卷完成成績排序并輸出前三名的功能
? ? public void three(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int num=0;
? ? ? ? for (int i=scores.length-1;i>=0;i--){
? ? ? ? ? ? if ((scores[i]>100)||(scores[i]<0)){
? ? ? ? ? ? ? ? continue; ??
? ? ? ? ? ? } ? ? ? ? ? ?
? ? ? ? ? ? num++;
? ? ? ? ? ? if (num>3){
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? System.out.println("第"+num+"名是:"+scores[i]); ??
? ? ? ? }
? ? }
}
2017-06-13
import java.util.Arrays;
import java.util.Scanner;
public class HelloWorld {
? ??
? ? public static void main(String[] args) {
? ? ? ??
?
? ? ? ?//定義一個(gè)數(shù)組,并從鍵盤輸入數(shù)據(jù)。
? ? ? ?int[] scores = new int[9];//這里你應(yīng)該定義數(shù)組的長度不然會報(bào)錯(cuò)
? ? ? ? for (int i=1;i<=9;i++){
? ? ? ? System.out.println("請輸入考試成績:");//錄入成績的提示代碼我們應(yīng)該放在循環(huán)內(nèi),不然什么都沒有不利于我們錄入信息
? ? ? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? ? ? int nums= input.nextInt(); ?
? ? ? ? ? ? scores[i-1] = nums;//如果你要i從1開始,scores[i-1]這里就應(yīng)該設(shè)為i-1因?yàn)閿?shù)組下標(biāo)從0開始
? ? ? ? }
? ? ? ? System.out.println("考試前三名成績?yōu)椋?);
? ? ? ? HelloWorld topthree=new HelloWorld(); ? ?
? ? ? ? topthree.three(scores);
? ? }
? ??
? ? //百分試卷完成成績排序并輸出前三名的功能
? ? public void three(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int num=0;
? ? ? ? for (int i=scores.length-1;i>=0;i--){
? ? ? ? ? ? if ((scores[i]>100)||(scores[i]<0)){
? ? ? ? ? ? ? ? continue; ??
? ? ? ? ? ? } ? ? ? ? ? ?
? ? ? ? ? ? num++;
? ? ? ? ? ? if (num>3){
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? System.out.println("第"+num+"名是:"+scores[i]); ??
? ? ? ? }
? ? }
}
程序基本沒什么問題,上面有我對你存在問題的說明和建議。