gpt4 book ai didi

java - 在java程序中的方法中计算最大投篮命中率

转载 作者:行者123 更新时间:2023-12-02 06:11:26 25 4
gpt4 key购买 nike

必须在一组球队投篮命中率数据中找到最大值和最小值。

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

System.out.print(" Player FGM FGA FGP%\n");
int count = 0;
for( int i = 0;in.hasNext(); i++)
{
firstname[i] = in.next();
lastname[i] = in.next();
fgm[i] = in.nextInt();
fga[i] = in.nextInt();
fgp = (1.0 * fgm[i]) / (1.0 * fga[i]) * 100;
count++;
System.out.printf("%10s %10s %3d %3d %3.1f \n",firstname[i],lastname[i],fgm[i],fga[i],fgp);
}
maxFGP = maxFGP(fgm,fga,count);
minFGP = minFGP(fgm,fga,count);
System.out.printf("\n\nThe player with the highest field goal percentage is: %3d ",maxFGP(fgm,fga,count));
System.out.printf("\nThe player with the lowest field goal percentage is : %3.1f",fgp);
}

public static int maxFGP(int[] fgm, int[] fga, int count)
{
int max = 0;
for(int i = 0; i < count; i++)
{
if((1.0 * fgm[i]) / (1.0 * fga[i]) * 100 > max)
max = i;
}
return max;
}



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

需要“if”语句的帮助才能返回正确的最大值。

我们有所有球员的百分比,但需要使用这些方法来找到投篮命中率最高和最低的球员。

这是我正在使用的数据文件:

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

最佳答案

应该是这样的:

public static int maxFGP(int[] fgm, int[] fga, int count)
{
int max = 0;
double maxValue=(1.0 * fgm[0]) / (1.0 * fga[0]) * 100;
for(int i = 0; i < count; i++)
{
if((1.0 * fgm[i]) / (1.0 * fga[i]) * 100 > maxValue)
{
max = i;
maxValue=(1.0 * fgm[i]) / (1.0 * fga[i]) * 100;
}
}
return max;
}

注意:您已经设置了 int max = 0;,因此无需从 i=0 循环。改为 int i=1

要打印具有最大值的玩家的名称:

System.out.printf("\n\nThe player with the highest field goal percentage is: %3d ",firstname[maxFGP(fgm,fga,count)]);

关于java - 在java程序中的方法中计算最大投篮命中率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21843505/

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