Results 21 to 31 of 31
- 05-24-2011, 10:40 AM #21
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
i see where your coming from but for some odd reason enter 1,2,3,4,5,6,7,8,9,10 and 0 to exit gives me 2 as smallest and 3 as second smallest. Shouldn't it be 1 and 2?
and now when i just enter 0 to exit i get those numbers i initialized at the beginning as output. i tried putting the print statement inside the if but since there's a loop it repeats it when it goes around.
- 05-24-2011, 10:44 AM #22
Don't put the print statement in the while loop.
You set your variables to the numbers I said? Post your updated code so I can see it.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-24-2011, 10:45 AM #23
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I kind of figured he probably wasn't very familiar with arrays/array lists. Just suggested it because the logic can be much simpler.
- 05-24-2011, 10:47 AM #24
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
I didn't put the print statement in it. I put the print statement inside the brackets of the ending of the last if statement.Java Code:import java.util.*; public class Project2Beta{ public static void main(String[] args){ //main method calls output method and executes it output(); //Variables must be integers int num, Larg=-2147483648, Small=2147483647, Small2=2147483647; Scanner in=new Scanner(System.in); num=in.nextInt(); while (num != 0){ output1(); num=in.nextInt(); if(num > Larg){ Larg=num;} if(num < Small2 && num > Small && num != 0){ Small2=num;} if(num < Small && num !=0){ Small2 = Small; Small=num; } } System.out.print("Largest: "+Larg+" \nSmallest: "+Small+" \nSecond Smallest: "+Small2); } //Method called output public static void output(){ System.out.print("Enter a number to begin executing Largest, Smallest and "); System.out.println("Second Smallest number program, Otherwise terminate program by entering 0"); } //Method called output1 public static void output1(){ System.out.print("Enter another number: "); } }
- 05-24-2011, 10:56 AM #25
The place where you are reading the input in while loop is causing the problem. Read the number at the end of the while loop, instead of in the beginning. Thats what is causing you to skip the first value.
Hope that helps,Java Code:while (num != 0){ output1(); [COLOR="red"]//num=in.nextInt(); Don't have this here[/COLOR] if(num > Larg){ Larg=num;} if(num < Small2 && num > Small && num != 0){ Small2=num;} if(num < Small && num !=0){ Small2 = Small; Small=num; } num=in.nextInt(); [COLOR="red"]// Have it here[/COLOR] }
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-24-2011, 10:56 AM #26
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-24-2011, 11:10 AM #27
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
Thank you all for your help. Really appreciate it.
Is there any place i could put the print statement so it still prints the output and if i enter zero at the beginning of the program it just exits without displaying anything?
- 05-24-2011, 11:14 AM #28
Write another if statement for it.
Java Code:if(!(Larg == -2147483648 && Small == 214748647 && Small2 == 214748647)){System.out.println("My print statement.");}Last edited by Dark; 05-24-2011 at 11:16 AM.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-24-2011, 11:23 AM #29
Keep the current code as it is. But if you want to exit if the first input itself is a zero then you can use System.exit(). Something like,
Use this just before your while loop, after you read the integer first time. You can also have any print statement inside the if statement for display purpose before exiting. :cool:Java Code:.... num = in.nextInt(); if (num == 0) { // optional print statement. System.exit(0); } while (num != 0) {...... //rest of the code
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-24-2011, 11:25 AM #30
Member
- Join Date
- May 2011
- Posts
- 29
- Rep Power
- 0
Thanks for everything. :)
- 05-24-2011, 11:30 AM #31
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
Similar Threads
-
2 Errors Please help em figure out what is wrong with this
By jwb4291 in forum Advanced JavaReplies: 16Last Post: 08-09-2010, 11:40 PM -
Cant figure out where went wrong.
By leviathan in forum New To JavaReplies: 15Last Post: 06-06-2010, 06:55 PM -
i have a small error but i can't figure out what is wrong [code provided]
By blueduiker in forum New To JavaReplies: 3Last Post: 01-11-2010, 06:48 AM -
[SOLVED] Can't figure out what's wrong
By roach_van in forum New To JavaReplies: 10Last Post: 09-05-2008, 07:09 AM -
Please Help, can't figure out what I'm doing wrong.
By tamik0 in forum New To JavaReplies: 2Last Post: 07-11-2008, 09:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks