0

对于我的任务,我应该制定一个程序来帮助决定棒球运动员的选秀。该程序提示侦察员输入有关玩家的信息,并将其存储在一个数组中。然后它将检查数组并显示 25 岁以下且平均击球次数为 0.280 或以上的球员列表。该列表必须按年龄排序。

哦,菜单是必需的。

我的问题是除了标题之外它没有给我任何输出!不是排序吗?if 语句不起作用吗?怎么了!?

玩家类如下所示:

public class players 
{
    String name;
    String position;
    int age;
    double average;
}

这是我的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BlueJays 
{    
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
    static players[] arr;

public static void main(String[] args) throws IOException 
{
    String quit = "n";
    while("n".equals(quit))
    {
        //display menu
        System.out.println("Toronto Blue Jays Drafting Program - Main Menu");
        System.out.println("1) Input Blue Jays data");
        System.out.println("2) Display possible draft choices");      
        System.out.println("3) Quit program");
        System.out.print("Please choose an option by inputting the number of your choice: ");
        String choiceString = br.readLine();
        int choice = Integer.parseInt(choiceString);

        if(choice == 1)
        {
            inputInfo();
        }else if(choice == 2)
        {
            sortInfo();
        }else if(choice == 3)
        {
            System.out.println("Are you sure you want to quit? (y/n) ");  
            quit = br.readLine();
        }else
        {
            System.out.println("Not a valid option.");
        }                  
    }
}

//method to input names of Blue Jays
public static void inputInfo() throws IOException
{  
    players temp = new players();
    System.out.print("How many players would you like to enter? ");
    int x = Integer.valueOf(br.readLine()).intValue();
    arr = new players[x];     

    //loop through players
    for(int i = 0; i < arr.length; i++)
    {

        System.out.println("Enter player information.");

        System.out.println("Input first and last name: ");
        String name = br.readLine();
        temp.name = name;       

        System.out.println("Input position: ");
        String position = br.readLine();
        temp.position = position;

        System.out.println("Input batting average (e.g. .246): ");
        String averageString = br.readLine();
        temp.average = Double.parseDouble(averageString);

        System.out.println("Input age: ");
        temp.age = Integer.parseInt(br.readLine());
        System.out.println(" ");

        // Copy the software name and quantity to the global variables
        arr[i] = temp;
    }   
}

//method to sort and display info
public static void sortInfo()
{              
    //sort by quantity
    for(int i = 0; i < arr.length; i++)
    {
        for(int j = i+1; j < arr.length; j++)
        {
            if(arr[i].age > arr[j].age)
            {
                players  temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
            }
        }

    }            
    System.out.println("Draft Choices 2013");        
    //output
    for(int i = 0; i < arr.length; i++)
    {
        if (arr[i].age <= 25 && arr[i].average >= 0.280)
        {
            System.out.println("Name: " + arr[i].name);    
            System.out.println("Age: " + arr[i].age);
            System.out.println("Position: " + arr[i].position);
            System.out.println("Batting average: " + arr[i].average);
            System.out.println("   ");        
        }            

    }
    }
}

我需要尽快提交,因此将不胜感激任何帮助!提前致谢!

4

1 回答 1

3

可能导致您出现问题的代码中的 WTF:

players temp = new players();
System.out.print("How many players would you like to enter? ");
int x = Integer.valueOf(br.readLine()).intValue();
arr = new players[x];     

//loop through players
for(int i = 0; i < arr.length; i++)
{

    System.out.println("Enter player information.");

    System.out.println("Input first and last name: ");
    String name = br.readLine();
    temp.name = name;       
     ....

}

您只创建一个players实例并重用它。因此,您没有(比如说)20 个不同players的数组,而是一个指向同一个对象(实例)的 20 次的数组。该实例数据将被连续覆盖,并且仅保存您放入的最后一个玩家的数据,如果age > 25由于您的过滤器而没有记录,则将被打印。

将实例创建移到for

System.out.print("How many players would you like to enter? ");
int x = Integer.valueOf(br.readLine()).intValue();
arr = new players[x];     

//loop through players
for(int i = 0; i < arr.length; i++)
{
    players temp = new players();  // <---- THIS

    System.out.println("Enter player information.");

    System.out.println("Input first and last name: ");
    String name = br.readLine();
    temp.name = name;       
     ....

}

于 2013-08-31T10:18:08.300 回答