Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Missing Loop, not sure what I need

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Yeehaw
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Missing Loop, not sure what I need

    import java.util.Scanner;
    public class Apartment 
    {
    public static void main(String[] args)
    {
    	int unit, rent, main, increase, min=0, ntotal=0, total=0, profit, irent=0;
    	int nunit=0, ncost, cost, max;
    	Scanner keyboard = new Scanner(System.in);
     
    	System.out.println("Enter the total number of apartment units:");
    	unit=keyboard.nextInt();
    	System.out.println("Enter the rent to occupy ALL the units:");
    	rent=keyboard.nextInt();
    	System.out.println("Enter the increase in rent that results in a vacant unit:");
    	increase=keyboard.nextInt();
    	System.out.println("Enter the amount to maintain a rented unit:");
    	main=keyboard.nextInt();
     
    	 total=(rent*unit);
    	 cost=unit*main;
         profit=total-cost;
     
     do 
      {
    	  nunit=(unit-1);
    	  irent=(increase+rent);
    	  ntotal=(irent*nunit);
    	  ncost=nunit*main;
    	  min=ntotal-ncost;
      } 
    while (profit>min);
     
     
     
     
     
    	System.out.println("With "+unit+" apartment units and $"+rent+" each to occupy ALL the units, ");
    	System.out.println("assuming that each increase of $"+increase+" results in one vacant unit, and");
    	System.out.println("assuming that it costs $"+main+" to maintain each unit ...");
     
    	System.out.println("The optimum rent to charge is "+irent);
    	System.out.println(nunit+" apartment units can be rented at this rate.");
    	System.out.println("This will yield a total of "+min+" per month.");
     
    	System.exit(0);
    }
    }

    I am trying to figure out what my other loop should be. I need the program to run and find max amount of profit. i think i need to add a for loop, but I am not sure.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Missing Loop, not sure what I need

    You post is very vague. I have no idea what you are trying to do.

    Calling a variable the same name as the method (main) only leads to confusion.

    Having a System.exit call as the last line of your code is pointless as the program is going to exit anyway.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    ZorraBella (October 28th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Yeehaw
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Missing Loop, not sure what I need

    Sorry for the vague description on what I am trying to do. an example of what I am trying to do is, take 50 apartments with $600 rent. The maintenance for an occupied apartment is $27. For every empty apartment the rent goes up $40. i am trying to get my program to tell me what is the least amount of apartments that can be rented for the maximum amount of profit. I had to use System.exit(0), because the program kept running. once I get it set the right way I will take it out, for I agree it will not be needed. I will change the variable main to maintenance, to make the program read better. thank you for the input.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Missing Loop, not sure what I need

    If I understand you need to calculate the rent when all apartments are empty and store the maxProfit.
    Then calculate the rent when 49 apartments are empty and 1 is occupied. Then compare if rent is greater than previous maxProfit.
    Then calculate the rent when 48 apartments are empty and 2 are occupied. Then compare if rent is greater than previous maxProfit.
    etc.
    Improving the world one idiot at a time!

  6. The Following User Says Thank You to Junky For This Useful Post:

    ZorraBella (October 28th, 2013)

  7. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Yeehaw
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Missing Loop, not sure what I need

    So it sounds like I was going at it backwards. I need to start the apartments at 1 and increase until I find the max profit. I will turn it around and see if I can get it to run. THANK YOU!!!!!!!

Similar Threads

  1. Missing Class?
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 18th, 2013, 07:49 AM
  2. [SOLVED] 'ELSE' missing 'IF'
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 30th, 2013, 10:51 AM
  3. What am I missing?
    By jean28 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2012, 10:49 AM
  4. What am I missing?
    By prgmrGrl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 9th, 2012, 11:33 PM
  5. missing something so simple....help!
    By b094mph in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2011, 10:12 PM

Tags for this Thread