Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Find Maximum Value from the value entered.

kenglekkenglek Posts: 5Member
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));

}

}

Comments

  • VilanyeVilanye Posts: 684Member
    : 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
  • kenglekkenglek Posts: 5Member
    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.
  • VilanyeVilanye Posts: 684Member
    : 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.
  • kenglekkenglek Posts: 5Member
    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.
Sign In or Register to comment.