Results 1 to 13 of 13
- 09-08-2011, 03:10 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
How to display largest and smallest numbers entered..?
Hi, I'm new to java programming and I have a particular program that I'm having trouble with. This is the description of the program.
"Write a program with a loop that lets the user enter a series of integers. The user should enter -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered."
So far I have this:
import java.util.Scanner;
public class Series
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a series of integer or -99 to end the series: ");
int integer = keyboard.nextInt();
while(integer != -99)
{
System.out.print("Enter a series of integer or -99 to end the series: ");
integer = keyboard.nextInt();
}
}
}
Ok, now I'm stuck because I can't seem to figure out how to display the largest and smallest numbers entered. Any help would be much appreciated. THank you.
- 09-08-2011, 03:13 PM #2
Re: How to display largest and smallest numbers entered..?
I don't see any code that does anything with the largest or smallest number.
How would you do this by hand, or in your head, without a computer? If somebody was going to tell you a bunch of numbers, how would you tell them the smallest and largest numbers when they were done?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 09-08-2011, 03:13 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: How to display largest and smallest numbers entered..?
You'll need a couple of int variables.
One to hold the largest and one to hold the smallest.
- 09-08-2011, 03:16 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Re: How to display largest and smallest numbers entered..?
thanks for the quick reply. I will try to modify this program and I hope I can figure it out thanks
- 09-08-2011, 04:19 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Re: How to display largest and smallest numbers entered..?
You'll probably want to do a for loop that steps through every number in your integer variable (Which you might want to change the name of, that can get confusing). Then just compare it to the largest and smallest number to see if it is the new largest or smallest, and if it is, store it as such. Remember that your first number is always going to be both the largest and smallest.
Two common things to forget here would be to not actually use the -99, and to account for someone making -99 the only number.Last edited by Taikei_no_Yuurei; 09-08-2011 at 04:36 PM.
- 09-08-2011, 04:26 PM #6
Re: How to display largest and smallest numbers entered..?
Gotta love spoonfeeders...
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 09-08-2011, 04:27 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: How to display largest and smallest numbers entered..?
No need to loop through anything other than the while loop already in the OP (unless I've misread the problem).
- 09-08-2011, 04:34 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Re: How to display largest and smallest numbers entered..?
Gotta love snide comments.
Yeah, you could also compare the numbers as they come in if you preferred, and then simply display the answer when the -99 is entered. That's a bit more efficient.
- 09-08-2011, 04:37 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Re: How to display largest and smallest numbers entered..?
Well I figured it out myself and somehow my code works. Here it is.
import java.util.Scanner;
public class Challenge8
{
public static void main(String [] args)
{
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, integer = 0;
Scanner keyboard = new Scanner(System.in);
while(integer != -99)
{
System.out.print("Enter a series of integer or -99 to end the series: ");
integer = keyboard.nextInt();
if(integer < min && integer != -99)
min = integer;
if(integer > max)
max = integer;
}
System.out.println(min);
System.out.println(max);
}
}
If u guys can find any errors here pls tell me. Thank you all!
- 09-08-2011, 04:42 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: How to display largest and smallest numbers entered..?
Type in the numbers -101, -100 and -99 and see what your program is doing ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-08-2011, 04:47 PM #11
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Re: How to display largest and smallest numbers entered..?
when i typed -101, -100 and -99 the largest number is -99 but it's not suppose to read that number. So i fixed it by modifying my code.
if(integer > max && integer != -99)
max = integer;
Is that what you're looking for JosAH?
- 09-08-2011, 04:51 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 09-08-2011, 05:03 PM #13
Re: How to display largest and smallest numbers entered..?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01-14-2011, 06:36 PM -
Array[] get smallest/largest value
By bobocheez in forum New To JavaReplies: 13Last Post: 09-09-2010, 11:16 PM -
finding the largest k numbers in an array without sorting the whole list?
By jmmjm in forum Advanced JavaReplies: 5Last Post: 02-07-2009, 07:48 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01-12-2008, 10:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks