Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的程序中的数组似乎有什么问题?[Java阵列(练习)]_Java - Fatal编程技术网

我的程序中的数组似乎有什么问题?[Java阵列(练习)]

我的程序中的数组似乎有什么问题?[Java阵列(练习)],java,Java,练习: (对学生进行排序)编写一个程序,提示用户输入学生人数、学生姓名及其分数,并按分数降序打印学生姓名 到目前为止我所做的: package chapter6; import java.util.Scanner; public class Chapter6 { public static void main(String[] args) { /*6.17*/ //prompt the user to enter the number of students

练习:

(对学生进行排序)编写一个程序,提示用户输入学生人数、学生姓名及其分数,并按分数降序打印学生姓名

到目前为止我所做的:

package chapter6;
import java.util.Scanner;

public class Chapter6 {


public static void main(String[] args) {

    /*6.17*/    
    //prompt the user to enter the number of students
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter the student number ="+" ");
    int number = input.nextInt();

    //array for the names and input
    String[] names = namesInput(number);
    //array for scores and input
    double[] score = scoreInput(number);

    //display students' names in decreasing order according to theri scores
    displayNamesForScores(names,score,number);

}

public static void displayNamesForScores(String[] name,double[] score ,int number)
{   
    for(int i =0;i<number;i++)
        {double temp=0;
         String str ;
         if(score[i+1]>score[i])
         {   temp = score[i];
             score[i]=score[i+1];
             score[i+1]=temp;

             str = name[i];
             name[i]=name[i+1];
             name[i+1]= str;

         }


        }
   System.out.println("Students:");
   for(int i=0;i<number;i++)
       {
         System.out.println(name[i]+" ");
       }

}

public static double[] scoreInput(int number)
{
Scanner input = new Scanner(System.in);
double[] score = new double[number];
 int cnt=1;
for(int i =0;i<score.length;i++)
    {

    System.out.print("Please enter the score for the"+" "+cnt+" "+"student="+" ");
    double scr = input.nextDouble();
    score[i]=scr;
    cnt++;

    }    

return score;
}

public static String[] namesInput(int number)

{
   Scanner input = new Scanner(System.in);
   String[] name = new String[number];
   int cnt=1;
   for(int i= 0;i<name.length;i++)

        {

        System.out.print("Enter the"+" "+cnt+" "+"name ="+" ");
        String str = input.nextLine();
        name[i]=str;
        cnt++;
        }

 return name;
  }
 }
包第6章;
导入java.util.Scanner;
公共课第六章{
公共静态void main(字符串[]args){
/*6.17*/    
//提示用户输入学生人数
扫描仪输入=新扫描仪(System.in);
System.out.print(“请输入学号=“+”);
int number=input.nextInt();
//名称和输入的数组
字符串[]名称=名称输入(数字);
//分数和输入的数组
double[]分数=分数输入(数字);
//根据分数按降序显示学生姓名
显示分数的名称(名称、分数、编号);
}
公共静态void displayNamesforCores(字符串[]名称、双[]分数、整数)
{   
for(int i=0;iscore[i])
{temp=分数[i];
分数[i]=分数[i+1];
分数[i+1]=临时工;
str=名称[i];
名称[i]=名称[i+1];
姓名[i+1]=str;
}
}
System.out.println(“学生:”);

对于(int i=0;i您将在此行获得
ArrayIndexOutOfBoundsException

if(score[i+1]>score[i]) // when i = number -1

因为当
i=number-1
时,
i+1
等于
number
并且该索引在数组中不可访问。数组的最大可访问索引始终是
array.length-1
。超出该值的任何内容都将抛出
ArrayIndexOutOfBoundsException
,您将获得
ArrayIndexOutOf此行的边界异常

if(score[i+1]>score[i]) // when i = number -1

因为当
i=number-1
时,
i+1
等于
number
并且该索引在数组中不可访问。数组的最大可访问索引始终是
array.length-1
。超出该值的任何内容都将抛出
ArrayIndexOutOfBoundsException
,我鼓励您学习使用调试ger(Eclipse的非常好);它将帮助您轻松准确地识别导致此类问题的原因。我鼓励您学习使用调试器(Eclipse的非常好);它将帮助您轻松准确地识别导致此类问题的原因。