Results 1 to 9 of 9
  1. #1
    russbuss106 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default Printing ranges of numbers using if/else statements

    I have to write a problem that take two integers as input from the user and print the range of numbers between them within square brackets. Print an increasing sequence if the first number is smaller than the second. Print a decreasing sequence if the second is smaller than the first. If the numbers are equal then one of them should be printed.
    Example:

    Input: 2 and 7
    Output: [2, 3, 4, 5, 6, 7]

    Input: 3 and -2
    Output: [3, 2, 1, 0, -1, -2]

    Input: 3 and 3
    Output: [3]

    Any help would be greatly appreciated. Thanks

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what have you tried so far? do you have a specific problem with your design?

  3. #3
    russbuss106 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    i'm actually starting from scratch. I am completely new to programming and am totally lost.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    are you taking a course, or are you teaching yourself? if you're taking a course, review what you've been taught so far and refer to your instructor/professor/ta.

    if you aren't taking a course, at this point, we can't do much more (at least nothing that'll actually help you advance your knowledge) than to refer you to tutorials to get you accustomed to using java BEFORE trying to even make a practical program. so, here's a basics tutorial:

    Trail: Getting Started (The Java™ Tutorials)

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

    Default

    I'd look into using a java.util.Scanner object to get input from the user. Then think of what type of loop you should use to display the output. If you are still totally lost, then have a look at the Sun tutorials here: Lesson: Language Basics

    Come on back if you have any specific questions regarding your code. If you are still lost, then perhaps have a chat with your teacher.

  6. #6
    russbuss106 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    thanks for your help. I can get the concept of being able to input the values but I just get lost in making exceptions so that the numbers will print in a series depending on the values you put in just as said in the example. Like inputing 2 and 7 will look like: [2, 3, 4, 5, 6, 7]

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    there is no reason to use exceptions in such a program. if a > b, count up. if a < b, count down. as far as how to count in the direction, there are a few ways you can do it, but that's up to you

  8. #8
    bigbigbug is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    try also use while loop :)

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    do wat emceenugget says, or you can do it the lazy way.
    use Math.min and Math.max, then just loop through the numbers.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  2. Printing Fibonacci Numbers
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:43 PM
  3. avoiding if statements
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 04-02-2008, 10:08 AM
  4. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 05:02 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:56 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
  •