需要帮助搞清楚如何确定最大和最小的数量

时间:2014-03-27 06:37:46

标签: java

我需要帮助,试图找出如何回答我在当地基督教青年会的教练给出的这个项目的最后两部分。我需要弄清楚球队最好和最差的罚球命中率是多少。我知道代码有点笨重,我确信有更好的方法来编写各种方法使其更顺畅,但我是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) +"%")
}   
}

1 个答案:

答案 0 :(得分:1)

首先编写randomShot,这是一种产生随机数的方法,并返回一个布尔值,表明镜头是否成功。

接下来,您想要找到一种方式来玩游戏&#34;并产生你需要的结果。有多种方法可以做到这一点:

  • 面向对象的方法是使用Game类和一种方法来玩游戏(使用randomShot方法),并提供访问器函数来获取游戏结果。一种方法是建立一个镜头结果数组,然后从中计算出你需要的东西。实际上还有另一种有点棘手的方式,它不能构建一系列镜头。

  • 执行此操作的一种功能方法是让一个除了保存游戏的结果之外什么都不做的类,以及获取详细信息的访问者,并拥有一个游戏方法返回GameResult个对象。这种方式允许您使用相同的技术。

  • 一种混合方法将游戏放入游戏构造函数中,这样每个游戏对象只能玩一个游戏,然后围绕着持有结果。

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