2014-03-27 50 views
0

我需要帮助,试图找出如何回答这个项目的最后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) +"%") 
} 
} 
+1

你是否熟悉数组?这些将有很大的帮助。更好的(为了您的教育)将使用ArrayList类。 – dfeuer

+0

此外,如果您在注释中指出每个变量的含义(除了像循环变量这样的小事),每个类代表什么以及每种方法应该做什么,您的代码将更容易理解。你的主要功能通常应该*很少*;最多可以处理IO;所有的计算都应该在别处发生。 – dfeuer

+0

我使用的数组非常有限,因此需要用整数预先定义数组块,然后调用它们来运行方法。我试图在此创建一个随机数生成器方法,但是当我尝试运行布尔值并计入它时,我无法使其生成正确数量的答复。 – planker1010

回答

1

开始写randomShot,产生一个随机数,并返回一个布尔值,表示一个镜头是否成功的方法。

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

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

  • 这样做将有一个类,什么也不做,但保持结果的游戏和存取方法,以获得详细信息,并有一个返回GameResult对象绘制而成的游戏方法的功能性的方式。这种方式可以让你使用这些相同的技术。

  • 一种混合的方法将游戏玩到游戏构造函数中,以便每个游戏对象只玩一个游戏,然后坐在保持结果周围。

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

相关问题