Dylan
Dylan

Reputation: 23

Java: Calculating and Storing total in a variable

I'm asked to calculate the total number of golf club members that are expected to play today (total multiplied by decimal percentage) and store that total in the players variable. I do not know how to store the total. Any help would be great. I tried using if statements then typing, players = input.nextDouble(); but that doesn't work. I'm very new to java...

 class golf
 {
     private int weather;
     private double val1;
     private double players;

     public void calculations()
     {            
         Scanner input = new Scanner(System.in);
         System.out.print("Enter the number of club members: "); 
         val1 = input.nextDouble();

         System.out.println("Now select a number for the weather");   //weather atm
         System.out.println("1: Sunny");
         System.out.println("2: Overcast");
         System.out.println("3: Rain");
         System.out.print("Selection: ");
         weather = input.nextInt();

         System.out.println(" ");
    }

    public void output()
    {
        if (weather == 1) //calculates expected players when Sunny
            System.out.println(val1*.25 + " " + "will play golf today!");

        if (weather == 2) //calculates expected players when Overcast
            System.out.println(val1*.12 + " " + "will play golf today!"); 

        if (weather == 3) //calculates expected players when Rainy
            System.out.println(val1*.03 + " " + "will play golf today!"); 
    }
}

Upvotes: 2

Views: 553

Answers (1)

Msp
Msp

Reputation: 2493

Is this what you need?

public void output() {
    if (weather == 1) {
        players = 0.25 * val1;
    }

    if (weather == 2) {
        players = 0.12 * val1;
    }

    if (weather == 3) {
        players = 0.03 * val1;
    }

    System.out.println(players + " will play golf today!");
}

Upvotes: 1

Related Questions