dimanche 18 mars 2018

Java: Preventing a cash prize from exceeding a certain amount

I am a beginner in Java programming and have finished learning about if-statements.

This is my code for running a race. An issue that I ran into recently with a similar code was inserting an if-statement so the prize money does not exceed 350. I have tested this code and everything else works as it determines a tie and sorts the runners with their prize amounts. The only issue is every-time I insert an if-statement to keep the prize amount from going over 350 the code does not run or messes up the other set if-statements.

if (reward>350)
 reward=350;

I have tried multiple attempts to make the code not exceed 350, but it usually passes through and the prize exceeds that amount. I have even put the statement in different areas of the code. Does anyone have any suggestions? - Author of code: K.D

    //import scanner class
import java.util.Scanner;

public class Runner {
public static void main(String[] args) 
{
    //declaring variables for user input, runners name and time.
    String runner1,runner2,runner3;
    int mins1;
    int mins2;
    int mins3;

    //stores reward amount
    double reward;

    Scanner keyboard = new Scanner(System.in); 


    System.out.print("What is the name of the first runner? ");
    runner1 = keyboard.nextLine();

    System.out.print("What is this runner's score in minutes? ");
    mins1 = keyboard.nextInt();
    keyboard.nextLine();

    System.out.print("What is the name of the second runner? ");
    runner2 = keyboard.nextLine();

    System.out.print("What is this runner's score in minutes? ");
    mins2 = keyboard.nextInt();

    keyboard.nextLine();
    System.out.print("What is the name of the third runner? ");
    runner3 = keyboard.nextLine();

    System.out.print("What is this runner's score in minutes? ");
    mins3 = keyboard.nextInt();



//determines who came in first,second and third
    if (mins1 < mins2 && mins1 < mins3 )
    { 

        if (mins2 < mins3)
        {
            System.out.println("\nThe first place runner is " + runner1 + "\n The second place runner is "
            + runner2 + "\n The third place runner is " + runner3 + ". ");
            reward= (60-mins1)*10;
            System.out.println(runner1+ " won the race!");
            System.out.printf("Cash prize: $%.2f", reward);
        }
        else
        {
            System.out.println("\nThe first place runner is " + runner1 + "\n The second place runner is "
            + runner3 + "\n The third place runner is " + runner2 + ". ");
            reward= (60-mins1)*10;
            System.out.println(runner1+ " won the race!");
            System.out.printf("Cash prize: $%.2f", reward);
        }
    }
    else if (mins2 < mins1 && mins2 < mins3)
    {
        if (mins1 < mins3)
        {
            System.out.println("\nThe first place runner is " + runner2 + "\n The second place runner is "
            + runner1 + "\n The third place runner is " + runner3 + ". ");
            reward= (60-mins2)*10;
            System.out.println(runner2+ " won the race!");
            System.out.printf("Cash prize: $%.2f", reward);
        }
        else
        {
            System.out.println("\nThe first place runner is " + runner2 + "\n The second place runner is "
            + runner3 + "\n The third place runner is " + runner1 + ". ");
            reward= (60-mins2)*10;
            System.out.println(runner2+ " won the race!");
            System.out.printf("Cash prize: $%.2f", reward);
        }
    }

    else if (mins3 < mins1 && mins3 < mins2)
    {
        if (mins1 < mins2)
        {
            System.out.println("\nThe first place runner is " + runner3 + "\n The second place runner is "
            + runner1 + "\n The third place runner is " + runner2 + ".");
            reward= (60-mins3)*10;
            System.out.println(runner3+ " won the race!");
            System.out.printf("Cash prize: $%.2f", reward);

        }
        else 
        {
            System.out.println("\nThe first place runner is " + runner3 + "\n The second place runner is "
                    + runner2 + "\n The third place runner is " + runner1 + ".");
            reward= (60-mins3)*10;
            System.out.println(runner3+ " won the race!");
            System.out.printf("Cash prize: $%.2f", reward);
        }

    }

    //an if-else-if statement that determines if there is a tie and distributes the prize money evenly between two runners.
    if (mins1==mins2)
    {
        reward= (60-mins1)*10/2;
        System.out.println(runner1 +" and "+ runner2 + " won the race!");
        System.out.printf("They both recieve: $%.2f", reward);
    }
    else if (mins2 == mins3)
    {
        reward= (60-mins2)*10/2;
        System.out.println(runner2 +" and "+ runner3 + " won the race!");
        System.out.printf("They both recieve: $%.2f", reward);
    }
    else if(mins3==mins1) 
    {
        reward= (60-mins3)*10/2;
        System.out.println(runner3 +" and "+ runner1 + " won the race!");
        System.out.printf("They both recieve: $%.2f", reward);

    }
}

}

Aucun commentaire:

Enregistrer un commentaire