Results 1 to 8 of 8
Thread: Need help with my java program.
- 05-19-2011, 08:58 PM #1
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
Need help with my java program.
I'm trying to write a program that keeps getting numbers from the user, until the user enters a 0.
The program then prints the smallest two numbers that were entered, and the largest number that was entered.
Example:
User enters the numbers 15, 33, 26, 12, 19, -5, 98, 12, 2, 0
The output will be
"Largest number:" 98
"Smallest number:" -5
"Second smallest number:" 2
So far this is what I've got but I'm lost now.
import java.util.*;
public class Beta{
public static void main(String[] args){
int num;
Scanner in=new Scanner(System.in);
System.out.println("Enter a number:");
num=in.nextInt();
for
if
System.out.println("Largest number:");
System.out.println("Smallest number:");
System.out.println("Second smallest number:");
}
}
The 'if' statement and 'for' loop i put there because i believe they would need to be used but i can't figure out what to put in them. Thanks for the help in advance. I'm new to programming so can you guys be clear please thanks.
- 05-19-2011, 09:39 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Forget about Java for the moment. Suppose I hand you over numbers, one by one; you have three boxes labeled 'largest', 'smallest' and 'second smallest'. After I've given you a number you can stick it in a box (or more than one). Then I hand you another number and another ... how would you do it by hand?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-19-2011, 09:44 PM #3
Ok, so there are several types of loops, each works in a slightly different way, and should be used in specific circumstances.
A 'for' loop is a counting loop. You use it when you want to do something n times, and the value of n is known.
A 'while' loop loops until a condition is met - while(x < y). Use this when the number of iterations is unknown, or if you want an infinite loop that only stops under certain conditions (hint hint).
A 'for each' loop (also known as fast iteration) loops for as many times as there is something in a collection. This is a variation on a regular for loop, has a simpler syntax, and is ideal for doing something with every item in a list. It looks like this:
A 'do while' loop is a variation on a regular while loop, except it does something first, and then checks if it should stop or continue (a normal while loop checks first, and then only does something if the conditions evaluate to true).Java Code:for(String s : someListOfStrings){ //do something }
Hope this helps clear up your confusion about loops and where to use them :D
- 05-19-2011, 10:10 PM #4
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
Well considering that I don't know how many numbers you'd be giving me I'd collect all of them till your done then place the largest, smallest and second smallest number in the correct box.
- 05-19-2011, 10:13 PM #5
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
Thanks, definitely cleared my understanding of loops.
- 05-19-2011, 10:15 PM #6
What if, instead of going through all the numbers at the end, you looked at each one as it was handed to you and compared it to the ones already in the boxes?
Get in the habit of using standard Java naming conventions!
- 05-19-2011, 10:22 PM #7
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
I'd replace what's in the box if the following number is greater or less than what's already inside the box.
- 05-19-2011, 10:47 PM #8
Right. So, in pseudocode:
define three boxes
do this:
- get a number from the user
- replace what's in the boxes if the number is greater or less than what's already inside
...until the user tells us to quit
display what's in the boxes
Now translate that into Java.
This is a good way to approach every programming problem: think about it in real-world terms, write pseudocode (if only in your head), and then translate to code.Last edited by kjkrum; 05-19-2011 at 10:49 PM.
Get in the habit of using standard Java naming conventions!
Similar Threads
-
Call one Java Program from another Java Program
By rajpalparyani in forum New To JavaReplies: 3Last Post: 02-14-2011, 04:13 AM -
Is There A Way To Call Another Java Program Within A Java Program
By SwissR in forum New To JavaReplies: 4Last Post: 07-30-2010, 12:25 PM -
execute java program within java program
By popey in forum New To JavaReplies: 2Last Post: 10-22-2009, 05:32 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks