gpt4 book ai didi

java - 篮球积分Java程序?

转载 作者:行者123 更新时间:2023-11-30 03:02:16 24 4
gpt4 key购买 nike

Write a program that determines all the different ways for a player to score a certain number of points in a basketball game. Three pointers are worth 3 points (duh), field goals are worth 2 points, and foul shots are worth 1 point each. Be sure to print the total number of combinations at the end.

Run:

How many points were scored? 8

2 three pointer(s), 1 field goal(s), 0 foul shot(s)

2 three pointer(s), 0 field goal(s), 2 foul shot(s)

1 three pointer(s), 2 field goal(s), 1 foul shot(s)

1 three pointer(s), 1 field goal(s), 3 foul shot(s)

1 three pointer(s), 0 field goal(s), 5 foul shot(s)

0 three pointer(s), 4 field goal(s), 0 foul shot(s)

0 three pointer(s), 3 field goal(s), 2 foul shot(s)

0 three pointer(s), 2 field goal(s), 4 foul shot(s)

0 three pointer(s), 1 field goal(s), 6 foul shot(s)

0 three pointer(s), 0 field goal(s), 8 foul shot(s)

There are 10 different ways to score 8 points

到目前为止我所拥有的:

import java.util.Scanner;
public class MarchMadness
{
public static void main(String[] args)
{
Scanner kbReader = new Scanner(System.in);
System.out.println("How many points were scored?");
int points = kbReader.nextInt();
int ft = 1;
int fg = 2;
int tp = 3;

if (points % tp == 0)
{
System.out.println((points/tp) + " three pointers.");
}


}
}

我知道如何使程序适用于特定情况,例如 8 个点,但我不知道如何使其接受任意数量的点并获得正确的输出或打印出可能的解决方案的数量。我该怎么办?

最佳答案

因为这是家庭作业,所以答案在最后,我希望你不需要它。

一次只阅读几行,尝试看看在你自己弄清楚之前需要阅读多少

<小时/>

假设您有多个点,例如 points=16

<强>1。多少个三分球?

你可以用制作多少个三指针?嗯,最多 points/3.0=5.333。我们将其四舍五入为 5。一般情况下:

int maxThreePointers=points/3;

java中的整数运算自动向下舍入。现在我们知道三个指针的数量必须在 0 .. maxThreePointers 范围内。现在我们知道了很多!

<强>2。投篮命中数是多少?

从上述范围中选择任意个数量的三指针,作为示例,让我们执行 ThreePointers = 2 的情况。考虑一下剩下要分配的点数。 int pointMissing = points - ThreePointers*3; 在我们的例子中 pointsMissing = 16 - 2*3 = 10

冲洗并重复第一个想法:我们现在有 10 分,我们最多可以有多少个射门得分? int maxFieldGoals = pointsMissing/2;,即示例中的 0 到 5 之间。

3.有多少次罚球?

从上述范围中选择任意数量的三分球和投篮命中数。剩下的分数必须是犯规进球。完成!

这可以在一组嵌套循环中进行编码:

<小时/>

扰流墙

<小时/>

扰流墙

<小时/>

扰流墙

<小时/>

扰流墙

<小时/>

扰流墙

<小时/>

扰流墙

<小时/>

扰流墙

<小时/>

扰流墙

<小时/>
int points = 16; // read this from stdin. 
int maxThreePointers = points/3;
int count = 0;

for( int threePointers = 0; threePointers <= maxThreePointers; threePointers++ ){
int remainingPoints = points - threePointers*3;
int maxFieldGoals = remainingPoints/2;
for( int fieldGoals = 0; fieldGoals <= maxFieldGoals; fieldGoals ++ ){
// last one is easy!
int foulShots = remainingPoints - fieldGoals*2;
count++;
System.out.println(threePointers + " three pointers, " + fieldGoals + " field goals and " + foulShots + " foul shots" );
}
}

System.out.println( "There are " + count + " ways to score " + points + " points" );

关于java - 篮球积分Java程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35659583/

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