Java 数组和排序
我的作业要求我编写一个程序,让用户输入10名球员的姓名、年龄、位置和平均击球率。(为了减少混乱,我只让程序输入了3名玩家)。然后,该程序应该只检查并显示25岁以下的球员的统计数据,这些球员的平均击球率为.280或更高,然后按年龄顺序显示 我的代码(如下所示)在选择选项2之前工作正常。它没有排序或显示任何内容。如果有人能帮我,那就太重要了。任何关于我的代码的总体建议都将非常有用。谢谢Java 数组和排序,java,arrays,sorting,Java,Arrays,Sorting,我的作业要求我编写一个程序,让用户输入10名球员的姓名、年龄、位置和平均击球率。(为了减少混乱,我只让程序输入了3名玩家)。然后,该程序应该只检查并显示25岁以下的球员的统计数据,这些球员的平均击球率为.280或更高,然后按年龄顺序显示 我的代码(如下所示)在选择选项2之前工作正常。它没有排序或显示任何内容。如果有人能帮我,那就太重要了。任何关于我的代码的总体建议都将非常有用。谢谢 import java.io.*; import java.util.Scanner; public cla
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
}
`
import java.io.*;
导入java.util.Scanner;
公共级蓝鸟{
静态字符串名称[]=新字符串[3];//可以容纳10个名称的名称数组
静态int age[]=new int[3];//可以容纳10个年龄的年龄数组
静态字符串位置[]=新字符串[3];//可容纳10个位置的位置数组
静态双平均[]=新双平均[3];//平均阵列可以容纳10个击球平均数
静态int x,i;
//主要方法
公共静态void main(字符串[]args)引发IOException{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int menuChoice=1;
System.out.print(“Hello and Wlecome to Blue Jay Java Sort”);
while(menuChoice!=3){
System.out.print(“\rEnter菜单选项\n”);
系统输出打印(“*************************”);
System.out.print(“\r(1)=>输入蓝色数据\n”);
System.out.print(“(2)=>显示可能的草稿选项\n”);
系统输出打印(“(3)=>退出\r”);
//为每个案例场景尝试catch语句
试一试{
menuChoice=Integer.parseInt(br.readLine());
}捕获(IOIE){
即printStackTrace();
}
开关(menuChoice){
案例1:
inputInfo();
打破
案例2:
输入排序();
打破
案例3:
返回;
}
}
}
public static void inputInfo()引发IOException{
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
扫描仪p=新扫描仪(系统英寸);
//循环以请求填充数组
对于(x=0;x<3;x++){
//询问玩家姓名
System.out.print(“\rEnter player全名:”);
//读取输入并在数组中存储名称
name[x]=in.readLine();
//询问球员年龄
系统输出打印(“输入玩家年龄:”;
//读取输入并在数组中存储年龄
年龄[x]=p.nextInt();
//询问球员的位置
系统输出打印(“输入玩家位置:”);
//读取输入并在数组中存储位置
位置[x]=in.readLine();
//询问球员的平均击球率
系统输出打印(“输入球员击球平均值:”);
//读取输入并在数组中存储击球平均值
平均值[x]=p.nextDouble();
}
}
公共静态void inputSort(){
int最小值,温度;
//选择排序
对于(x=0;x<3-1;++x){
最小=x;
对于(i=x+1;i<10;++i){
if(年龄[i]<年龄[最小]){
最小=i;
}
}
温度=年龄[x];
年龄[x]=年龄[最小];
年龄[最小]=温度;
}
System.out.println(“姓名”+“----”+“年龄”+“----”+“位置”+“----”+“击球平均值”);
对于(x=0;x<3;x++){
如果(年龄[x]=0.280){
System.out.println(名称[x]+“----”+年龄[x]+“----”+位置[x]+“----”+平均值[x]);
}
}
//关闭主管道()
}
//密级
}
`
修改:
在这里,您需要对您的程序进行一个小的更改,如下所示:
inputSort()
方法中,需要将for循环条件从(i=x+1;i<10;++i)的更改为(i=x+1;i<3;++i)
李>
您必须在声明ArrayIndexOutofBond
时出错,因为您试图访问一个不存在的索引值
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()
}
}
公共类蓝鸟{
静态字符串名称[]=新字符串[3];//可以容纳10个名称的名称数组
静态int age[]=new int[3];//可以容纳10个年龄的年龄数组
静态字符串位置[]=新字符串[3];//可容纳10个位置的位置数组
静态双平均[]=新双平均[3];//平均阵列可以容纳10个击球平均数
静态int x,i;
//主要方法
公共静态void main(字符串[]args)引发IOException{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int menuChoice=1;
System.out.print(“Hello and Wlecome to Blue Jay Java Sort”);
while(menuChoice!=3){
System.out.print(“\rEnter菜单选项\n”);
系统输出打印(“*************************”);
System.out.print(“\r(1)=>输入蓝色数据\n”);
System.out.print(“(2)=>显示可能的草稿选项\n”);
系统输出打印(“(3)=>退出\r”);
//为每个案例场景尝试catch语句
试一试{
menuChoice=Integer.parseInt(br.readLine());
}捕获(IOIE){
即printStackTrace();
}
开关(menuChoice){
案例1:
inputInfo();
打破
案例2:
输入排序();
打破
案例3:
返回;
}
}
}
public static void inputInfo()引发IOException{
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
扫描仪p=新扫描仪(系统英寸);
//循环以请求填充数组
对于(x=0;x<3;x++){
//询问玩家姓名
系统输出(