Results 1 to 5 of 5
  1. #1
    radio is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default Counting numbers up and down

    Hi my name is John,

    basically i try to write a program that can print this
    2
    3
    4
    5
    4
    3
    2
    1

    but the i can print from 1 to 5 and i dont know how to go from 4,3,2,1 etc...

    Java Code:
    /** 
     * Write a description of class Counting here.
     * @author (your name)
     * @version (a version number or a date) 
     */
    
    public class Counting
    {    
        // instance variables - replace the example below with your own
        public int z = 5;    
        /**     
         * Constructor for objects of class Counting 
         */
        public Counting()
        {
            for (int x = 5; x >=1; x--)
            {
                for (int y = 1; y<=5; y++)  
                {
                    System.out.println(y);
                }
                System.out.println(x);
            }     
        }
            
          public static void main(String [] args)
          {
              Counting ncounting = new Counting();
          }
    }
    i am very new to java, a help would be greatful.

    thanks in advance

    John

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    reverse the for loop
    Java Code:
    		for (int i = 5; i >= 0; i--) {
    			System.out.println(i);
    		}

  3. #3
    radio is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default

    hi there,

    thanks for the reply but i've tried your method it didnt worked also

    can you rather than giving the hint, please re-correct my code, as i am new to java, i dont understand the clue's or hint's..3

    thanks

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Being new to java doesnt mean that you cant spend a little time thinking, or actually attempt to solve the problem:

    Java Code:
    public class Counting {
    	// instance variables - replace the example below with your own
    	public int z = 5;
    
    	/**
    	 * Constructor for objects of class Counting
    	 */
    	public Counting() {
    		for (int y = 1; y <= 5; y++) {
    			System.out.println(y);
    		}
    		
    		for (int x = 5; x >= 1; x--) {
    			System.out.println(x);
    		}
    	}
    
    	public static void main(String[] args) {
    		Counting ncounting = new Counting();
    	}
    }

  5. #5
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by radio View Post
    can you rather than giving the hint, please re-correct my code, as i am new to java, i dont understand the clue's or hint's..3

    thanks
    So you can cheat by passing out code off as your own? You don't even deserve a D.

    You've been given more than sufficient help to resolve your problem, as long as you've paid attention in class and gone through the tutorials.

Similar Threads

  1. Compare 5 numbers
    By Snowboardmylife in forum New To Java
    Replies: 5
    Last Post: 04-15-2008, 07:04 PM
  2. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 AM
  3. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM
  4. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 05:51 PM
  5. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 05:45 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
  •