gpt4 book ai didi

java - 对存储的数组中的特定值进行排序和显示

转载 作者:行者123 更新时间:2023-12-01 23:36:47 24 4
gpt4 key购买 nike

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

哦,菜单是必需的。

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

players 类如下所示:

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(" ");
}

}
}
}

我需要很快将其交给您,因此我们将不胜感激!提前致谢!

最佳答案

代码中的 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 个不同玩家的数组,而是一个指向同一对象(实例)20 次的数组。该实例数据将不断被覆盖,并且仅保存您输入的最后一个玩家的数据,并且如果它的年龄> 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;
....

}

关于java - 对存储的数组中的特定值进行排序和显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18546885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com