java - 数组和排序

标签 java arrays sorting

我的作业要求我编写一个程序,让用户输入 10 名球员的姓名、年龄、位置和击球率。 (为了减少困惑,我让程序只输入3个玩家)。然后,程序应该仅检查并显示那些 25 岁以下且击球率为 0.280 或更高的玩家的统计数据,然后按年龄顺序显示它们。

我的代码(如下所示)在选择选项 2 之前一直运行良好。它不排序或显示任何内容。如果有人能帮助我,那就意义重大。关于我的代码的任何总体建议也将非常有帮助。谢谢。

import java.io.*;  
import java.util.Scanner;

public class BlueJays {
static String name[] = new String[3]; //Name Array that can hold 10 names
static int age[] = new int[3]; //Age Array that can hold 10 ages
static String position[] = new String[3]; //Position Array that can hold 10 positions
static double average[] = new double[3]; //Average Array the can hold 10 batting averages
static int x, i;

//Main Method
public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int menuChoice = 1;
    System.out.print("Hello and Wlecome to Blue Jay Java Sort");

    while (menuChoice != 3) {

        System.out.print("\rEnter Menu Choice\n");
        System.out.print("**********************");
        System.out.print("\r(1) => Enter Blue Jay Data \n");
        System.out.print("(2) => Display Possible Draft Choices \n");
        System.out.print("(3) => Exit \r");

        //try-catch statement for each case scenario
        try {

        menuChoice = Integer.parseInt(br.readLine());
        } catch (IOException ie) {
            ie.printStackTrace();
        }

        switch(menuChoice) {
        case 1:
            inputInfo();
            break;
        case 2:
            inputSort();
            break;
        case 3:
            return;
        }

    }

}

     public static void inputInfo() throws IOException { 

     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

     Scanner p = new Scanner(System.in); 

     //loop to request to fill array   
        for (x = 0; x < 3; x++) {

        //Ask for player name
        System.out.print("\rEnter player full name: ");
        //Read input and store name in an Array
         name[x] = in.readLine();

        //Ask for player age  
        System.out.print("Enter age of player: ");
        //Read input and store age in an Array
        age[x] = p.nextInt();

        //Ask for position of player
        System.out.print("Enter player position: ");
        //Read input and store position in an Array
        position[x] = in.readLine();

        //Ask for batting average of player
        System.out.print("Enter batting average of player: ");
        //Read input and store batting average in an Array
        average[x] = p.nextDouble();


        }

     }


        public static void inputSort() {

        int smallest, temp;

        //Selection Sort
        for (x = 0; x < 3 - 1; ++x) {

        smallest = x;

        for(i = x + 1; i < 10; ++i) {

            if (age[i] < age [smallest]) {
                smallest = i;
            }

            }

            temp = age [x];
            age [x] = age [smallest];
            age [smallest] = temp;

        }

        System.out.println(" Name " + " -----" + " Age " + "-----" + " Position "  + "-----" + "Batting Average "); 

        for (x = 0 ; x < 3; x++) {

        if (age[x] <= 25 && average[x] >= .280) {



                   System.out.println( name[x] + " ----- " + age[x] + " ----- " + position[x] + " ----- " + average[x]);


            }

        }

    //Close Main()  
    }

  //Close Class
 }
    `

最佳答案

修改:

这里您需要对程序进行一些小的更改,如下所示:

  1. inputSort()方法,您需要将 for 循环条件从 for(i = x + 1; i < 10; ++i) 更改为至for(i = x + 1; i < 3; ++i)

您一定收到一条错误消息 ArrayIndexOutOfBound因为您试图访问不存在的索引值。

public class BlueJays {
    static String name[] = new String[3]; //Name Array that can hold 10 names
    static int age[] = new int[3]; //Age Array that can hold 10 ages
    static String position[] = new String[3]; //Position Array that can hold 10 positions
    static double average[] = new double[3]; //Average Array the can hold 10 batting averages
    static int x, i;

    //Main Method
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int menuChoice = 1;
        System.out.print("Hello and Wlecome to Blue Jay Java Sort");

        while (menuChoice != 3) {

            System.out.print("\rEnter Menu Choice\n");
            System.out.print("**********************");
            System.out.print("\r(1) => Enter Blue Jay Data \n");
            System.out.print("(2) => Display Possible Draft Choices \n");
            System.out.print("(3) => Exit \r");

            //try-catch statement for each case scenario
            try {
                menuChoice = Integer.parseInt(br.readLine());
            } catch (IOException ie) {
                ie.printStackTrace();
            }

            switch(menuChoice) {
            case 1:
                inputInfo();
                break;
            case 2:
                inputSort();
                break;
            case 3:
                return;
            }
        }
    }

    public static void inputInfo() throws IOException { 

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        Scanner p = new Scanner(System.in); 

        //loop to request to fill array   
        for (x = 0; x < 3; x++) {

            //Ask for player name
            System.out.print("\rEnter player full name: ");
            //Read input and store name in an Array
            name[x] = in.readLine();

            //Ask for player age  
            System.out.print("Enter age of player: ");
            //Read input and store age in an Array
            age[x] = p.nextInt();

            //Ask for position of player
            System.out.print("Enter player position: ");
            //Read input and store position in an Array
            position[x] = in.readLine();

            //Ask for batting average of player
            System.out.print("Enter batting average of player: ");
            //Read input and store batting average in an Array
            average[x] = p.nextDouble();
        }
    }


    public static void inputSort() {

        int smallest, temp;

        //Selection Sort
        for (x = 0; x < 3 - 1; ++x) {

            smallest = x;

            for(i = x + 1; i < 3; ++i) {
                if (age[i] < age[smallest]) {
                    smallest = i;
                }
            }

            temp = age [x];
            age [x] = age [smallest];
            age [smallest] = temp;
        }

        System.out.println(" Name " + " -----" + " Age " + "-----" + " Position "  + "-----" + "Batting Average "); 

        for (x = 0 ; x < 3; x++) {
            if (age[x] <= 25 && average[x] >= .280) {
                System.out.println( name[x] + " ----- " + age[x] + " ----- " + position[x] + " ----- " + average[x]);
            }
        }

        //Close Main()  
    }
}

关于java - 数组和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50539498/

相关文章:

java - java 中的正则表达式 字母数字!@#$+。 :=&*_-

c++ - g++ 4.8.5 负数组索引的循环优化错误

javascript - 使用相同的键动态合并对象数组

javascript - 使用lodash javascript全连接二维数组

java - 处理循环的 EJB 事务超时

java - mysql连接器字符集结果不支持utf8mb4

javascript - 使用 JavaScript/JQuery,您可以根据列的 <td> 类名对 HTML 表格列进行排序(升序/降序)吗?

scala - 如何按值升序对 Scala 映射进行排序?

javascript - Jquery 排序列表元素

java - 中断Java中的循环线程