我需要幫助,試圖找出如何回答這個項目的最後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