# Thread: Printing ranges of numbers using if/else statements

1. Member
Join Date
Mar 2009
Posts
3
Rep Power
0

## 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. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
9
what have you tried so far? do you have a specific problem with your design?

3. Member
Join Date
Mar 2009
Posts
3
Rep Power
0
i'm actually starting from scratch. I am completely new to programming and am totally lost.

4. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
9
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. 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. Member
Join Date
Mar 2009
Posts
3
Rep Power
0
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. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
9
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. Member
Join Date
Mar 2009
Posts
6
Rep Power
0
try also use while loop :)

9. do wat emceenugget says, or you can do it the lazy way.
use Math.min and Math.max, then just loop through the numbers.

#### Posting Permissions

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