Audizine - An Automotive Enthusiast Community

Results 1 to 12 of 12
  1. #1
    Veteran Member Four Rings
    Join Date
    Feb 11 2004
    AZ Member #
    85
    Location
    Seattle/Seoul

    java programming help...

    Guest-only advertisement. Register or Log In now!
    i have this for loop:

    for (int i = 1; i <= 10; i++) {
    System.out.println(i);
    }

    and want to:

    Show a revision of this code that counts from 1 to 10 but skips the number 7. Don't use more than one loop.

    i'd appreciate any help

    thanks

    peace
    DBC ownz joo!! <- Wow what year is that from??

  2. #2
    Veteran Member Three Rings voodooA4's Avatar
    Join Date
    Feb 11 2004
    AZ Member #
    92
    Location
    USA

    how about:

    for (int i = 1; i <= 10 && i != 7; i++)
    {
    System.out.println(i);
    }

    (That for loop is valid in C/C++ so I imagine it'd be fine in java as well, since they share a fairly similar syntax)
    98.5 Cactus Green A4 Quattro, APR Chipped
    114K Miles

    Open Source ECU: http://groups.yahoo.com/group/openecu/
    OpenDiag Group: http://groups.yahoo.com/group/opendiag/

  3. #3
    Veteran Member Four Rings
    Join Date
    Feb 11 2004
    AZ Member #
    85
    Location
    Seattle/Seoul

    hmmm... ran it in dr. java and it goes up to 6 and stops...

    thanks for the help tho, any other ideas to get it to just skip 7?

    also, in boolean arthimetic, what does: || mean?

    thanks

    peace
    Last edited by tigabalm; 07-13-2004 at 04:26 PM.
    DBC ownz joo!! <- Wow what year is that from??

  4. #4
    Veteran Member Three Rings voodooA4's Avatar
    Join Date
    Feb 11 2004
    AZ Member #
    92
    Location
    USA

    d'oh! I shoulda known that...simple mistake. I'll figure out another version for you...

    anyway, in boolean algebra, you have three simple operators: AND, OR, and NOT (there's some others, but they're mixtures of these three)

    In C-style languages, they're written like so:

    AND is &&
    OR is ||
    NOT is !
    98.5 Cactus Green A4 Quattro, APR Chipped
    114K Miles

    Open Source ECU: http://groups.yahoo.com/group/openecu/
    OpenDiag Group: http://groups.yahoo.com/group/opendiag/

  5. #5
    Veteran Member Four Rings
    Join Date
    Feb 11 2004
    AZ Member #
    85
    Location
    Seattle/Seoul

    voodoo u rock!!!

    thanks so much!!!

    peace
    DBC ownz joo!! <- Wow what year is that from??

  6. #6
    Veteran Member Three Rings voodooA4's Avatar
    Join Date
    Feb 11 2004
    AZ Member #
    92
    Location
    USA

    are you allowed to use an 'if' statement inside the loop? I can't think of a way to do it otherwise...except maybe XOR'ing the numbers with 7, but that seems too ridiculous for this case...

    if you're allowed to, then:

    for (int i = i; i <= 10; i++)
    {
    if (i == 7)
    {
    continue;
    }
    else
    {
    System.out.println(i);
    }
    }
    Last edited by voodooA4; 07-13-2004 at 06:52 PM.
    98.5 Cactus Green A4 Quattro, APR Chipped
    114K Miles

    Open Source ECU: http://groups.yahoo.com/group/openecu/
    OpenDiag Group: http://groups.yahoo.com/group/opendiag/

  7. #7
    Veteran Member Three Rings quicksilver_S's Avatar
    Join Date
    Feb 11 2004
    AZ Member #
    67
    Location
    City that never sleeps

    try

    for (int i = 1; i <= 8 && i >=1; i++)
    {
    System.out.println(i);
    }

  8. #8
    Veteran Member Three Rings voodooA4's Avatar
    Join Date
    Feb 11 2004
    AZ Member #
    92
    Location
    USA

    that would only print the numbers 1 thru 8, no? unless I'm missing something...
    98.5 Cactus Green A4 Quattro, APR Chipped
    114K Miles

    Open Source ECU: http://groups.yahoo.com/group/openecu/
    OpenDiag Group: http://groups.yahoo.com/group/opendiag/

  9. #9
    Veteran Member Four Rings
    Join Date
    Feb 11 2004
    AZ Member #
    85
    Location
    Seattle/Seoul

    well doesnt say that i cant use an if statement so i think we are good to go using voodoo's 2nd attempt.... one correction tho: i think its supposed to be:

    int i = 1 and not: int i = i right?

    THANKS GUYS!! really appreciate the help!

    peace
    DBC ownz joo!! <- Wow what year is that from??

  10. #10
    Veteran Member Three Rings voodooA4's Avatar
    Join Date
    Feb 11 2004
    AZ Member #
    92
    Location
    USA

    no problem!

    yep, it should be i = 1
    98.5 Cactus Green A4 Quattro, APR Chipped
    114K Miles

    Open Source ECU: http://groups.yahoo.com/group/openecu/
    OpenDiag Group: http://groups.yahoo.com/group/opendiag/

  11. #11
    Veteran Member Four Rings
    Join Date
    Feb 11 2004
    AZ Member #
    85
    Location
    Seattle/Seoul

    another question:

    if i'm supposed to evaluate a "boolean expression"

    how am I supposed to do:

    (x > 3) || z

    thanks

    peace
    DBC ownz joo!! <- Wow what year is that from??

  12. #12
    Veteran Member Three Rings voodooA4's Avatar
    Join Date
    Feb 11 2004
    AZ Member #
    92
    Location
    USA

    what do you mean 'evaluate it'...how so?
    98.5 Cactus Green A4 Quattro, APR Chipped
    114K Miles

    Open Source ECU: http://groups.yahoo.com/group/openecu/
    OpenDiag Group: http://groups.yahoo.com/group/opendiag/

Bookmarks

Posting Permissions

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


    © 2001-2025 Audizine, Audizine.com, and Driverzines.com
    Audizine is an independently owned and operated automotive enthusiast community and news website.
    Audi and the Audi logo(s) are copyright/trademark Audi AG. Audizine is not endorsed by or affiliated with Audi AG.