我需要帮助,试图找出如何回答这个项目的最后2部分,我是由当地基督教青年会的教练给出的。我需要弄清楚球队出手罚球的最佳和最差的比赛。我知道代码有点笨拙,我相信有更好的方法来编写各种方法来使它更流畅,但我是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) +"%")
}
}
你是否熟悉数组?这些将有很大的帮助。更好的(为了您的教育)将使用ArrayList类。 – dfeuer
此外,如果您在注释中指出每个变量的含义(除了像循环变量这样的小事),每个类代表什么以及每种方法应该做什么,您的代码将更容易理解。你的主要功能通常应该*很少*;最多可以处理IO;所有的计算都应该在别处发生。 – dfeuer
我使用的数组非常有限,因此需要用整数预先定义数组块,然后调用它们来运行方法。我试图在此创建一个随机数生成器方法,但是当我尝试运行布尔值并计入它时,我无法使其生成正确数量的答复。 – planker1010