Results 1 to 5 of 5
Thread: Counting numbers up and down
- 12-01-2007, 05:46 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 2
- Rep Power
- 0
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...
i am very new to java, a help would be greatful.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(); } }
thanks in advance
John
- 12-01-2007, 06:17 PM #2
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
reverse the for loop
Java Code:for (int i = 5; i >= 0; i--) { System.out.println(i); }
- 12-01-2007, 06:49 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 2
- Rep Power
- 0
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
- 12-01-2007, 07:08 PM #4
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
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(); } }
- 05-06-2011, 03:03 PM #5
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
Similar Threads
-
Compare 5 numbers
By Snowboardmylife in forum New To JavaReplies: 5Last Post: 04-15-2008, 07:04 PM -
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02-07-2008, 10:09 AM -
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:56 AM -
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11-29-2007, 05:51 PM -
Counting Vowels and Constonants
By MattN in forum New To JavaReplies: 3Last Post: 11-20-2007, 05:45 PM


LinkBack URL
About LinkBacks


Bookmarks