lundi 29 février 2016

If/Else nested in while loop issue

I'm a beginner at Java, and I thought a great way to learn was to build a simple basketball game.

The point of the basketball game is press 1 to shoot and 2 to pass. It will use random numbers 1-20 to determine the distance in feet you are from the basketball. Random numbers 21-24 are for you get fouled or turn the ball over.

For some reason, it seems like once the if statements begin, the program breaks down. Also, I'm not sure if I'm using the Random and Scanner correctly.Can you help me out with the code?

import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome to the basketball game!");
        System.out.println("How to play: If you want to shoot, press 1. Then choose the strength of your shot. Your" +
                " shooting strength is a percentage of how hard you want to shoot. Or you can pass the ball by pressing 2.");
        System.out.println("Get ready, it's time for tip-off!");
        int possession = 10;
        int score = 0;

        while (possession > 0) {
            Random distance = new Random();
            int Low = 1;
            int High = 25;
            int Result = distance.nextInt(High - Low) + Low;
            Scanner input = new Scanner(System.in);
            int choose = input.nextInt();

            System.out.println("Your team has the ball.");


            if (Result == 21) {
                System.out.println("You were fouled. You shoot two free throws, but only MADE 1 out of 2.");
                possession--;
                score++;
                System.out.println("SCORE: " + score + "-");
            } else if (Result == 22) {
                System.out.println("You were fouled. You shoot two free throws and you made 2 out of 2!");
                possession--;
                score = score + 2;
                System.out.println("SCORE: " + score + "-");
            } else if (Result == 23) {
                System.out.println("You were fouled. You shoot two free throws and you MISSED both!");
                possession--;
                System.out.println("SCORE: " + score + "-");
            } else if (Result == 24) {
                System.out.println("Your opponent stole the ball from you. TURNOVER.");
                possession--;
                System.out.println("SCORE: " + score + "-");
            } else {
                System.out.println("Your team has the ball. You are " + Result + " feet from the basketball.");
                System.out.println("What do you want to do? Press 1 to shoot or press 2 to pass:" + choose);
            }


        }
    }
}

Aucun commentaire:

Enregistrer un commentaire