0

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?

4
Contributors
3
Replies
20
Views
2 Years
Discussion Span
Last Post by oussama_1
Featured Replies
  • "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 Read More

0

What is the problem (or your expectation)?

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

1

"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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.