Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2008, 10:21 PM   #1
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 54
Rep Power: 1 kewlgeye is on a distinguished road
How to skip a number in java

Hello to all.

I have this code for my class and I what I am trying to do is decrement the number 39 by two so that odd numbers show, until I get to zero using the "for loop" The issue is that I am suppose to exclude the numbers 33, and 3 when it prints the numbers out. I can't figure that part out, and I can't find it anywhere in the chapter that I had to read on this. Can someone please help me understand how i should do this? This is the code I have so far, but I took out the extra for statements for clarity, beacuse I thought they would help me but they don't.

If I am suppose to use if statements, I don't know how to do this excluding 3 and 33.



Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class loop
  4. {
  5.  
  6. public static void main (String[] args)
  7. {
  8.  
  9. int i;
  10.  
  11.  
  12. for (i = 39; i >= 0; i = i - 2)
  13.  
  14.  
  15. System.out.print(i + " ");
  16.  
  17. System.out.println();
  18. }
  19. }

Last edited by big_k105; Apr 10th, 2008 at 7:43 PM.
kewlgeye is offline   Reply With Quote
Old Apr 9th, 2008, 10:29 PM   #2
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 54
Rep Power: 1 kewlgeye is on a distinguished road
Re: How to skip a number in java

Hey everyone, I figured it out about 5 minutes after I asked. Sorry, but thanks to those who would have helped anyway. Here is the solution that I found worked.

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class loop
  4. {
  5.  
  6. public static void main (String[] args)
  7. {
  8.  
  9. int i;
  10.  
  11.  
  12. for (i = 39; i >= 35; i = i - 2)
  13.  
  14.  
  15. System.out.print(i + " ");
  16.  
  17. for (i = 31; i >= 5; i = i - 2)
  18.  
  19.  
  20. System.out.print(i + " ");
  21.  
  22.  
  23. for (i = 1; i >= 0; i = i - 2)
  24.  
  25.  
  26. System.out.print(i + " ");
  27.  
  28.  
  29. System.out.println();
  30. }
  31. }

Last edited by big_k105; Apr 10th, 2008 at 7:44 PM.
kewlgeye is offline   Reply With Quote
Old Apr 9th, 2008, 10:54 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: How to skip a number in java

There's a simpler way to do it, using an if statement. Here's the pseudocode:
loop i from 39 to 0 step 2:
    if i != 33 and i != 3:
        print i + " "
    end if
end loop

By the way, to enclose Java code, you should do this:
Quote:
[code=java]
// insert code here
[/code]
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 10th, 2008, 6:35 PM   #4
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 257
Rep Power: 2 Fall Back Son is on a distinguished road
Re: How to skip a number in java

did you mean "every time the digit 3 is attained, ignore it"? or just the values 33 & 3? If its any digit, 3, then you might want to use some kind of toString() then get rid of the 3 then toInt()
Fall Back Son is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 10:58 AM
Java Entering a Number or Letter kewlgeye Java 9 Apr 5th, 2008 3:55 PM
Number in Java RainMan Java 5 Mar 31st, 2008 7:38 PM
Special browser in Java (Project) stalefish Java 3 Feb 9th, 2008 4:22 PM
Guess a Number 3n! C++ 7 Dec 2nd, 2007 3:38 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC