Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Find Maximum Value from the value entered. Posted by kenglek on 27 Jun 2005 at 10:11 AM
May I know how to find the Maximum value from the integers which being entered? As I search from a book, it did specify MAX_VALUE as a keyword, but I still cannot get it. Please help me, thanks.

import java.io.*;
class numbers
{
public static void main (String args[]) throws IOException
{
BufferedReader koay = new BufferedReader (new InputStreamReader (System.in));
int B, MN=0;
int total=0;
float n=0;

do
{

System.out.print("Type an integer: ");
String input = koay.readLine();
B = Integer.parseInt(input);

if(B>=1)
{
total=total + B ;
n=n+1;
}
}

while (B!=0);
System.out.println("The max value is " );
System.out.println("The sum value is " + total + " and the average is " + (total/n));

}

}

Report
Re: Find Maximum Value from the value entered. Posted by Vilanye on 27 Jun 2005 at 11:45 AM
: May I know how to find the Maximum value from the integers which being entered? As I search from a book, it did specify MAX_VALUE as a keyword, but I still cannot get it. Please help me, thanks.
:
: import java.io.*;
: class numbers
: {
: public static void main (String args[]) throws IOException
: {
: BufferedReader koay = new BufferedReader (new InputStreamReader (System.in));
: int B, MN=0;
: int total=0;
: float n=0;
:
: do
: {
:
: System.out.print("Type an integer: ");
: String input = koay.readLine();
: B = Integer.parseInt(input);
:
: if(B>=1)
: {
: total=total + B ;
: n=n+1;
: }
: }
:
: while (B!=0);
: System.out.println("The max value is " );
: System.out.println("The sum value is " + total + " and the average is " + (total/n));
:
: }
:
: }
:
:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html
Report
Re: Find Maximum Value from the value entered. Posted by kenglek on 27 Jun 2005 at 6:01 PM
Thanks. But this one I tried it before, it just stated the max value. How about the max value which entered from user's input? If user input lots of integers, how can we find out which number has the highest value?

Thank you.
Report
Re: Find Maximum Value from the value entered. Posted by Vilanye on 27 Jun 2005 at 6:10 PM
: Thanks. But this one I tried it before, it just stated the max value. How about the max value which entered from user's input? If user input lots of integers, how can we find out which number has the highest value?
:
: Thank you.
:

Ok, sorry for misunderstanding.

There are several ways to go about it.

One way is to create an int value, call it highest or something similar. Initialize it to the integer min, or add it code to make sure the first value entered becomes highest first value. Then as each number is entered, compare the new number to highest, if the new value is higher then assign that value to highest.

Another way is to put each value into an array(use a vector or linked list if you don't know how many values will be entered), when the user is done, you can do one of two things. Sort the array and grab the value, it will be in either the first or last element, depending on how it was sorted. Or you can walk through it and do comparisions like the first paragraph, but that is not that efficient.

Of these two ways, all will work, but sticking them into an array and sorting them would be more efficient, and less coding on your part, unless you write the sort yourself.
Report
Re: Find Maximum Value from the value entered. Posted by kenglek on 29 Jun 2005 at 7:09 AM
Thanks a lot Vilanye. :) I can get the Max value which I entered. At the same time, I able to get the Min value too.

Thank you so much, Vilanye.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.