gpt4 book ai didi

java - 生成包含球员姓名、FGM、FGA 和投篮命中率(即 FGP)的输出报告

转载 作者:行者123 更新时间:2023-12-01 13:25:05 24 4
gpt4 key购买 nike

我已经编译了代码,但出现了运行时错误:java.util.InputMismatchException:null(in.java.util.Scanner)

这是我的以下代码:

import java.io.*;
import java.util.Scanner;
public class kstatebball
{
public static void main(String [] args)
throws FileNotFoundException, IOException
{
Scanner inFile = new Scanner(new File("data1.txt"));
int count = 1;
int fgm[] = new int [100];
int fga[] = new int [100];
double fgp = 0;
int read,maxFGP,minFGP;

read = readArray(fgm,fga,count,inFile);
maxFGP = maxFGP(fgm,fga,count);
minFGP = minFGP(fgm,fga,count);


System.out.print("Player FGM FGA FGP%\n");
System.out.printf("%15d %3d %3d %2.1f",count,fgm,fga,fgp);
System.out.printf("The player with the highest fieldgoal percantage is: %3d",fgp);
System.out.printf("The player with the lowest field goal percantage is : %3d",fgp);

inFile.close();
}

static int readArray(int [] fgm, int [] fga, int count, Scanner inFile)
{
double temp = inFile.nextDouble();
int k;
int v;
v = inFile.nextInt();
for(k = 0; (v!=-999) && (k<count); k++)
{
fgm[k] = v;
v=inFile.nextInt();
}
return k;
}

public static int maxFGP(int[] fgm, int[] fga, int count)
{
for(int i = 0; i < 13; i++)
{
if(fgm[i] < fgm[count])
count = i;
}
return count;
}

public static int minFGP(int[]fgm, int[]fga, int count)
{

for(int i = 0; i > 13; i++)
{
if(fgm[i] > fgm[count])
count = i;
}
return count;
}
}

here is my data file I am reading in:
Marcus Foster 123 288
Thomas Gipson 102 178
Shane Southwell 88 224
Will Spradling 58 144
Wesley Iwundu 53 111
Nino Williams 49 96
Nigel Johnson 28 80
Jevon Thomas 15 58
D.J. Johnson 34 68
Omari Lawrence 27 65
Shawn Meyer 2 4
Ryan Schultz 2 9
Jack Karapetyan 1 4
Brian Rohleder 1 2
-999

我试图让我的输出如下所示:

    Marcus Foster    123    288        42%
Thomas Gipson 102 178 57%
Shane Southwell 88 224 39%
Will Spradling 58 144 40%
Wesley Iwundu 53 111 47%
Nino Williams 49 96 51%
Nigel Johnson 28 80 35%
Jevon Thomas 15 58 25%
D.J. Johnson 34 68 50%
Omari Lawrence 27 65 41%
Shawn Meyer 2 4 50%
Ryan Schultz 2 9 22%
Jack Karapetyan 1 4 25%
Brian Rohleder 1 2 50%

投篮命中率最高的球员是托马斯·吉布森,投篮命中率为 57%。

投篮命中率最低的球员是 Ryan Schultz,为 22%

最佳答案

当您调用方法(如 nextDoublenextInt())时,java.util.Scanner 类会抛出 java.util.InputMismatchException 错误,当读取的数据与您告诉它读取的类型不匹配时。请参阅http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextDouble()

根据您的输入文件,您错误地尝试将字符串读取为 double 。

static int readArray(int [] fgm, int [] fga, int count, Scanner inFile)
{
double temp = inFile.nextDouble();
int k;
int v;
v = inFile.nextInt();
for(k = 0; (v!=-999) && (k<count); k++)
{
fgm[k] = v;
v=inFile.nextInt();
}
return k;
}

关于java - 生成包含球员姓名、FGM、FGA 和投篮命中率(即 FGP)的输出报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21837659/

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