2014-03-27 48 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對象繪製而成的遊戲方法的功能性的方式。這種方式可以讓你使用這些相同的技術。

  • 一種混合的方法將遊戲玩到遊戲構造函數中,以便每個遊戲對象只玩一個遊戲,然後坐在保持結果周圍。

最後,您需要一種方法來播放五個遊戲並將結果呈現給用戶。再次,玩遊戲和建立一個結果數組是一個合理的方法。再一次,你實際上可以通過重構代碼來避免構建這個數組,但是它又可能有點煩人。

相關問題