Results 1 to 3 of 3
  1. #1
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default Printing a table with a for loop help

    hello, i am trying to write a program that displays the world population for present and the next 75 years based on its annual rate of increase (1.17). below is my code and its output. as you can see i am on the right track but i still cant figure out how to get the population to print next to the year, and also how to get it to repeat all the way down. that big number at the bottom of the list is the world population value that should be next to year 1. any clues?

    Java Code:
    public class WorldPopulation {
    
    	public static void main(String[] args) {
    		
    		long pop=6816330827L;
    		long population;
    		double rate=1.17;
    		{System.out.printf("%s%20s\n", "Year", "Population");
    		
    		for (int year=1; year<=75; year++)
    			
    			System.out.println(year);
    
    			
    		population = (long)(pop*rate);
    		
    			System.out.println(population);
    		
    
    	
    		}
    		
    		
    	}
    
    }
    output:

    Year Population
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    7975107067

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Use the printf method to format each line.

    As for why it's last, it's because your println is AFTER the loop. So, only after you output the first 75 values will it appear.
    Last edited by CodesAway; 10-31-2009 at 01:18 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 04:40 PM
  2. Problem printing inside FOR loop
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 06:02 PM
  3. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  4. Printing Help...
    By chiragkini in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2009, 07:07 AM
  5. Printing Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •