Results 1 to 9 of 9
- 03-04-2009, 10:38 PM #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
- 03-04-2009, 10:44 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
what have you tried so far? do you have a specific problem with your design?
- 03-04-2009, 10:50 PM #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.
- 03-04-2009, 11:16 PM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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)
-
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, 02:18 AM #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]
- 03-05-2009, 02:33 AM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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, 03:55 AM #8
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
try also use while loop :)
- 03-05-2009, 05:03 AM #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.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads
-
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Printing Fibonacci Numbers
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:43 PM -
avoiding if statements
By valoyivd in forum New To JavaReplies: 1Last Post: 04-02-2008, 09:08 AM -
Help with actionPerformed Statements
By wco5002 in forum New To JavaReplies: 8Last Post: 03-26-2008, 04:02 AM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 07:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks