I have the following code:

import java.util.Scanner;

public class WhileLoop {

    public static void main(String[] args) {

        int number;
        int maxValue = Integer.MAX_VALUE;
        int minValue = Integer.MIN_VALUE;

        Scanner input = new Scanner (System.in);

        System.out.println("Enter the length of numbers");
        int loop = input.nextInt();

        for(int i=1; i<=loop; i++){

            System.out.println("Enter a number: ");
            number = input.nextInt();

        if (number > maxValue) {
            maxValue = number;
            }
        if (number < minValue){
            minValue = number;
            }
            }
        System.out.println("Maximum Value is : " + maxValue);
        System.out.println("Minimum Value is : " + minValue);       
            }   
        }

its showing the result:
Maximum Value is : 2147483647
Minimum Value is : -2147483648

Can any buddy help me out?

Recommended Answers

All 3 Replies

What is the problem (or your expectation)?

My guess is you want to initialize maxValue and minValue the other way around.

He guesses right.

"the other way around":

 for(int i=1; i<=loop; i++){
System.out.println("Enter a number: ");
number = input.nextInt();

if(i==1){
maxValue = number;
minValue = number;
}
else{
if (number > maxValue) {
maxValue = number;
}
if (number < minValue){
minValue = number;
}}}

Good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.