-1

I am trying to get a basic java program running but it is not adding up and its giving me an infinite loop. Any help will be greatly appreciated.

Input : age of each patron (end of input denoted by a value < 0)

There are two categories of patrons:

Age 0 through 5 : Kids – no charge($0)

Age 5 through 18 : Students - $5

I am supposed to

  1. Get the age of the patron

  2. Determine which category that patron falls into & increase the number for that category

  3. Keep doing steps 1 & 2 until the end of input is reached

  4. Once end of input is reached • Calculate the revenue generated for each category (number of patrons in a category * rate for that category) • Total revenue for the day (sum of revenues of all 4 categories)

  5. Generate output in the following format

• Perot Museum : Today’s Revenue

• Number of kids : *****

• Revenue from kids : $0

• Number of students : ****

• Revenue from students : $****

This is my code:

import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
        int kidsPrice = 0;
        int studentPrice=5;
        
        
        Scanner input = new Scanner(System.in);
        int number = input.nextInt();
      
        
        while(number > 0 ){
            if (number >= 0 && number <= 5){
                for(int i = 0; i < number; i++)  {
                int  numOfKids = i;  
                System.out.println("Kids " + numOfKids);
                }
             System.out.println("Kids " + kidsPrice); 
              
            }
             
           else if (number >= 5 && number <= 18){
                for(int i = 0; i < number; i++)  {
                int  numOfStudents = i;   
                }
              System.out.println("Students");
           }
           --number;  
        } 

  
    
    }
}

It is not really doing any of the things I want, it is not adding the numbers and I need some pointers on what I am doing wrong and need to fix

Community
  • 1
  • 1
lenany
  • 75
  • 1
  • 2
  • 8
  • 1
    Can you give me an example of the input you would like? Eg would you like to input amount of kids, or input the age and have the program work out untill a negative age is entered – Ryan Turnbull Oct 02 '17 at 02:24
  • 1
    For problems like this take pretend you're the computer and trace through what would happen, if you entered in a value of say, 3. You'd see it taking the input and going into the while loop. What does it do after that? Does it go back and get more input? Does it only consider the number being passed in for a single condition? Does it change the number in any way? – Craig Taylor Oct 02 '17 at 02:29
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Oct 02 '17 at 02:29
  • Please read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) before attempting to ask more questions. –  Oct 02 '17 at 02:33
  • Quoting Eric Lippert's excellent essay [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/): "StackOverflow is a question-and-answer site for specific questions about actual code; “I wrote some buggy code that I can’t fix” is not a *question*, it’s a *story*, and not even an interesting story." – Daniel Pryden Oct 02 '17 at 02:43

1 Answers1

2

Your attempt is halfway aligned but does not follow the problem statement for few reasons. Try and improve on these lines:-

  1. Get the age of the patron

You've just read the number of patron, input their age within the while iteration and place your ifs on that age.

  1. increase the number for that category

this can be achieved by a ++numberOfKids for example in one your if conditions

  1. revenue generated for each category(number of patrons in a category * rate for that category)

Once you have values from (2) above. You know the rate(currently unused in your code), just to the maths as stated.

  1. Total revenue for the day (sum of revenues...

The revenues that you would calcaulate in (3), add them to attain these.

Naman
  • 27,789
  • 26
  • 218
  • 353