vendredi 3 mars 2017

Smallest and Largest numbers

Okay, I know there are several threads with similar questions to this, but I can't seem to find the answer to my problem on them. Everyone else is doing the same thing as me but their code works:

Heres the desired output:

How many numbers do you want to enter?
4
Enter a number:5
Enter a number:11
Enter a number:-2
Enter a number:3
Highest number is: 11
Lowest number is: -2

Here is my code:

public static void smallestLargest(){
    int highest=Integer.MIN_VALUE;
    int lowest=Integer.MAX_VALUE;
    int num=0;
    Scanner sL = new Scanner(System.in);
    System.out.println("How many numbers do you want to enter?");
    int nums = sL.nextInt();
    for(int i=0; i<nums; i++){
        System.out.print("Enter a number:");
        num = sL.nextInt();
    }
    if (num > highest){
        num = highest;
    }
    if(num < lowest){
        num = lowest;
    }
    System.out.println("Highest number is: " + highest);
    System.out.println("Lowest number is: " + lowest);

}

Heres the output I'm getting:

How many numbers do you want to enter?
4
Enter a number:5
Enter a number:11
Enter a number:-2
Enter a number:3
Highest number is: -2147483648
Lowest number is: 2147483647

I've got my friend on teamviewer trying to help since he's better at this than I am, but both of us are stumped. Thanks for your help.

Aucun commentaire:

Enregistrer un commentaire