Hi,

I am trying to do an assignment about buying zoo tickets but I am becoming stuck on the last hurdle when trying to get the accumulate number.

import java.util.Scanner;

public class ZooTaking {

	public static void main(String[] args) {
	    // Create a Scanner
	    Scanner input = new Scanner(System.in);
	    
	    // Ask if a new group is to be added
	    System.out.print("Enter a group? (Yes=1/No=0): ");
	    int i = input.nextInt();
	    int sum = 0;
	   
	    while (i != 0) {
	    	 sum += i;
	    	 
	    // Enter the number of accompanied children
	    System.out.print("Enter the number of accompanied children (age 6-15): ");
	    int accompaniedChildren = input.nextInt();
	    
	    // Enter the number of unaccompanied children
	    System.out.print("Enter the number of unaccompanied children (age 6-15): ");
	    int unaccompaniedChildren = input.nextInt();
	    
	    // Enter the number of adults 
	    System.out.print("Enter the number of adults (age 16-59): ");
	    int adult = input.nextInt();

	    // Enter the number of seniors
	    System.out.print("Enter the number of seniors (age 60+): ");
	    int senior = input.nextInt();
	    
	    // Calculate total entry charge
	    int totalTakings = (accompaniedChildren * 1) + (unaccompaniedChildren * 5) + (adult * 10) + (senior * 7);
	    	    
	    // Display results
	    System.out.println("Total entry charge is $" + totalTakings);
	    	
	    System.out.print("Enter a group? (Yes=1/No=0): ");
	    i = input.nextInt();
	    
	    }
	      
	    System.out.println("Total takings: $" + sum);
	    
	}
}

I am trying to get the Total takings, once the program has ended, if the program is looped many times. I keep on getting how many times the program is looped but not how much the total takings should be.

Any help will be greatly appreciated.

Thanks.

Recommended Answers

All 4 Replies

Then add to the sum the totalTakings

Then add to the sum the totalTakings

I tried that but it doesn't give the correct result. Is this right:

System.out.println("Total takings: $" + sum);

Sorry, i'm still trying to learn.

I tried that but it doesn't give the correct result. Is this right:

System.out.println("Total takings: $" + sum);

Sorry, i'm still trying to learn.

declare a new variable to store the totals and add totalTakings to it after every loop.

int total=0;
 total+= totalTakings;

that should help if that's what u're going for

Yes that helps.

Thanks very much...I get it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.