3 Replies - 306 Views - Last Post: 02 March 2013 - 01:06 AM Rate Topic: -----

#1 glr  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 20
  • Joined: 20-February 13

for loop help

Posted 02 March 2013 - 12:24 AM

I just started learning loops in java and given this exercise: Suppose that the tuition for a university is $40,000 this year and tuition increases 3% every year. Write a program that uses a for loop to compute the tution in 10 years.

public class Exercise2{
 public static void main(String[] args){
 int sum=0;
 int fee;
 
  //Prints 10 statements
  for(int i=1; i<11; i++){
  for(fee=1; fee<11; fee=(40000*(3/100)))  //Calculates the fees
  fee++;
  System.out.println("The tution fee for year "+i+" is "+fee); //Results
  
   }


  }
 }


This is my code so far and now I'm stuck on how to use the for loop to calculate the fee each year. Help?

Its not working correctly I need help

Is This A Good Question/Topic? 0
  • +

Replies To: for loop help

#2 Hishermen  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 01-March 13

Re: for loop help

Posted 02 March 2013 - 12:35 AM

int tuition = 40000;
for(int i = 1; i < 11, i++)
{
   tuition = tuition + (tuition * .03); 
   System.out.println("Tuition for year" + i + "is" + tuition;
}



In this code it goes threw 10 times. Each time it sets the tuition to .03% higher than what is was previously. Hope this is what you were looking for.
Was This Post Helpful? 2
  • +
  • -

#3 Limecore  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 91
  • Joined: 28-November 12

Re: for loop help

Posted 02 March 2013 - 12:42 AM

You screwed up the 2nd loop
it would (ignoring the fact that it would stop after the first iteration because 4000083/100 is greater than 11)
first make fee = 1, then 2, then 4000083/100, then 4000083/100+1, then 4000083/100+1, then 4000083/100 + 1
I think you may be able to do it with one loop

...
	for(double i=1, fee = 40000; i<=10;i++,fee=fee*1.03){
			System.out.println("The fee for year " + i + " is $"+ fee);
		}

Was This Post Helpful? 1
  • +
  • -

#4 glr  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 20
  • Joined: 20-February 13

Re: for loop help

Posted 02 March 2013 - 01:06 AM

I have this way that I use to format double values to 2 decimal places but its not working in the loops. why?

tuition=(int)(tuition*100)/100.0;


Nvm its working just wrong placement. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1