# Printing ranges of numbers using if/else statements

Printable View

• 03-04-2009, 11:38 PM
russbuss106
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
• 03-04-2009, 11:44 PM
emceenugget
what have you tried so far? do you have a specific problem with your design?
• 03-04-2009, 11:50 PM
russbuss106
i'm actually starting from scratch. I am completely new to programming and am totally lost.
• 03-05-2009, 12:16 AM
emceenugget
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)
• 03-05-2009, 12:30 AM
Fubarable
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.
• 03-05-2009, 03:18 AM
russbuss106
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]
• 03-05-2009, 03:33 AM
emceenugget
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
• 03-05-2009, 04:55 AM
bigbigbug
try also use while loop :)
• 03-05-2009, 06:03 AM
angryboy
do wat emceenugget says, or you can do it the lazy way.
use Math.min and Math.max, then just loop through the numbers.