-1

Hope this question makes sense but essentially here is the issue I am having. I am tasked to create a program that takes someone's free throw shooting percentage as an input then simulates 5 games where they attempt to shoot 10 free throws each game. As well as have a summary afterward showing things like best game, worst game, total made through all games, and average free throw percentage.

I currently have up to the point where I am trying to make my simulated game run multiple times but cant seem to figure it out. This is what I have so far: import java.util.*;

public class FreeThrow {
    public static int simulate(int input){
        int i;
        int j;
        int count = 0;

        for (j = 1; j < 6; j++){
            System.out.println("Game " + j + ":");      
            for(i = 0;i < 10; i++){
                int shot = (int)(Math.random()*101)-1;
                if (shot > input){
                    System.out.print("OUT ");//shot missed
                } else {
                    System.out.print("IN ");//shot made
                    count++;
                }
            }
            //prints the number of free throws made per game out of 10
            System.out.print("\nFree throws made: " + count + " out of 10.");
            return i;
        }
        return j;
    }
    public static void main (String[] args){
        //asks for user input to detemine player free throw percentage
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Player's Free Throw Percentage: ");
        int input = scan.nextInt();

        simulate(input);

    }
}

As you can see I currently have a for loop inside a for loop. I did this to try to make the simulated game loop loop itself again while adding the "Game 1:" line that would display above and show what game each was. It works perfectly for just one Game. I have it looking exactly like it should. As well here is an example of what the professor wants it to look like:Link To Image Any sort of insight on what I might be doing wrong or suggestions on how to get it to do what I would like would be greatly appreciated.

LordSherman
  • 33
  • 1
  • 8
  • 4
    You return at the end of the first iteration of the `j` loop, when you write `return i;`. Either remove the `return`, or remove the loop. – Andy Turner Mar 05 '18 at 22:49
  • Thank you so much that worked now time to iron out the way it displays and move on appreciate the help. – LordSherman Mar 05 '18 at 22:57
  • 2
    you should take the time to find out [how to use a debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems), as this would have made it easy to find the problem. – Andy Turner Mar 05 '18 at 23:01
  • Okay I will familiarize myself with this as I continue on in this assignment. – LordSherman Mar 05 '18 at 23:03

1 Answers1

0

I've added to your code so there is now a percentage made and a total, you pretty much had it correct, just had to make a small change so that count could be carried and displayed. If you want to have a best / worst game, you'd need to create two new variables, and update them for each game if the threshold was 1) beaten for best game, and 2) lower for worst game.

If you get stuck with this let me know, and i'll give you a hand. Should be easy enough for you to implement with what you currently know.

The issue was that you were returning i when it was not necessary either. That has been ameneded in this:

public class freeThrow {

private static int count;

    public static int simulate(int input){
        int i;
        int j;


        for (j = 1; j < 6; j++){
            System.out.println("Game " + j + ":");
            for(i = 0;i < 10; i++){
                int shot = (int)(Math.random()*101)-1;
                if (shot > input){
                    System.out.print("OUT ");//shot missed
                } else {
                    System.out.print("IN ");//shot made
                    count++;
                }
            }

            //prints the number of free throws made per game out of 10
            System.out.println("\nFree throws made: " + count + " out of 10.");
        }
        return j;
    }

    public static int average(int count) {
        int average = count/5;
        System.out.println("\nAverage is " +  average*10 + "%");
        return average;
    }

    public static int totalShots(int count) {
        int total = count;

        System.out.println("total shots made " + total);

        return total;
    }

    public static void main (String[] args){
        //asks for user input to detemine player free throw percentage
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Player's Free Throw Percentage: ");
        int input = scan.nextInt();

        simulate(input);
        average(count);
        totalShots(count);

    }
}
Connor J
  • 540
  • 1
  • 6
  • 17
  • Awesome thanks thanks, was able to get it all working to the point where I am counting separately the shots per game as well as counting the totals. Now I will work on the best/worst games. Do you by any chance have any suggestions where to start to do that? I would think I would need to create a variable for each separate game yet I don think that is feasible considering what I have now. – LordSherman Mar 06 '18 at 00:31
  • one of the easier ways would be to create two new int variables for bestGame and worstGame. Then just compare the value of how many shots were made in that game against each, if its higher than bestGame then it overrides it, or if its lower than worstGame then it overrides that value. – Connor J Mar 07 '18 at 11:29