gpt4 book ai didi

java - 需要帮助弄清楚如何确定最大和最小计数

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

我需要帮助,试图弄清楚如何回答本地基督教青年会的教练给我的这个项目的最后两个部分。我需要弄清楚球队罚球最好和最差的一场比赛是哪一场。我知道代码有点笨重,并且我确信有更好的方法来编写各种方法以使其更流畅,但我对 Java 很陌生,并且不断出错。谢谢。

import java.util.Scanner;
import java.util.Random;
public class Final {
public static void main(String[] args) {
//Create input scanner for shooter percentage
Scanner input = new Scanner(System.in);
System.out.print("Enter Player's Free Throw Percentage: ");
int percent = input.nextInt();
int inCount = 0;

// game and total make/miss counts
int outCount = 0;
int Game1 = 0;
int Game2 = 0;
int Game3 = 0;
int Game4 = 0;
int Game5 = 0;

// Game random num compared to user input percent and count for game and total
System.out.println("Game 1:");
Random r = new Random();
for (int i = 0; i < 10; ++i){
int randomInt = r.nextInt(100);
if (percent >= randomInt)
{
System.out.print("In" + " ");
inCount++;
Game1++;
}
if (percent < randomInt)
{
System.out.print("Out" + " ");
outCount++;
}
}
System.out.println();
System.out.println("Free throws made: " + Game1 + " out of 10.");

// Game random num compared to user input percent and count for game and total

System.out.println("");
System.out.println("Game 2:");
Random r2 = new Random();
for (int i = 0; i < 10; ++i){
int randomInt = r2.nextInt(100);
if (percent >= randomInt)
{
System.out.print("In" + " ");
inCount++;
Game2++;
}
if (percent < randomInt)
{
System.out.print("Out" + " ");
outCount++;
}
}
System.out.println();
System.out.println("Free throws made: " + Game2 + " out of 10.");
System.out.println("");

// Game random num compared to user input percent and count for game and total
System.out.println("");
System.out.println("Game 3:");
Random r3 = new Random();
for (int i = 0; i < 10; ++i){
int randomInt = r3.nextInt(100);
if (percent >= randomInt)
{
System.out.print("In" + " ");
inCount++;
Game3++;
}
if (percent < randomInt)
{
System.out.print("Out" + " ");
outCount++;
}
}
System.out.println();
System.out.println("Free throws made: " + Game3 + " out of 10.");
System.out.println("");

// Game random num compared to user input percent and count for game and total
System.out.println("");
System.out.println("Game 4:");
Random r4 = new Random();
for (int i = 0; i < 10; ++i){
int randomInt = r4.nextInt(100);
if (percent >= randomInt)
{
System.out.print("In" + " ");
inCount++;
Game4++;
}
if (percent < randomInt)
{
System.out.print("Out" + " ");
outCount++;
}
}


System.out.println();
System.out.println("Free throws made: " + Game4 + " out of 10.");
System.out.println("");

// Game random num compared to user input percent and count for game and total
System.out.println("");
System.out.println("Game 5:");
Random r5 = new Random();
for (int i = 0; i < 10; ++i){
int randomInt = r5.nextInt(100);
if (percent >= randomInt)
{
System.out.print("In" + " ");
inCount++;
Game5++;
}
if (percent < randomInt)
{
System.out.print("Out" + " ");
outCount++;
}
}


System.out.println();
System.out.println("Free throws made: " + Game5 + " out of 10.");
System.out.println("");

System.out.println("Summary:");
//System.out.println("Best Game Free Throws Made:" + );
//System.out.println("Worst Game Free Throws Made:");
System.out.println("Total Free Throws Made: " + (50-outCount) + " " + "out of 50");
System.out.println("Average Free Throw Percentage:" + (inCount*2) +"%")
}
}

最佳答案

首先编写randomShot,这是一种生成随机数并返回指示射击是否成功的 boolean 值的方法。

接下来,您想要想出一种“玩游戏”并产生您需要的结果的方法。有多种方法可以做到这一点:

  • 面向对象的方法是使用带有玩游戏方法的 Game 类(使用 randomShot 方法),并提供访问器函数来获取游戏结果。一种方法是构建一系列射击结果,然后从中计算出您需要的内容。实际上还有另一种稍微棘手的方法,它不会构建一系列镜头。

  • 实现此目的的一种功能性方法是拥有一个类,该类除了保存游戏的结果之外什么也不做,并且使用访问器来获取详细信息,并拥有一种游戏玩法返回一个 GameResult 对象。通过这种方式,您可以使用相同的技术。

  • 一种混合方法将游戏放入 Game 构造函数中,以便每个游戏对象只玩一个游戏,然后保存结果。

最后,您需要一种可以玩五场游戏并将结果呈现给用户的方法。再次强调,玩游戏并用结果构建数组是一种合理的方式。同样,您实际上可以通过重组代码来避免构建该数组,但这可能有点烦人。

关于java - 需要帮助弄清楚如何确定最大和最小计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22680031/

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