java - Cannot get correct max and min values of user inputs -
I am writing a method that takes the integer input to the user and displays total, average, maximum, and minimum.
I have a total and average job but I am getting 2147483647 for the maximum and for the minimum -2147483648
The loop should end only when user input -1.
My code:
Public static void processing points () {menu m = new menu (); Clrscr (); Int count = 0; // times of int has been added = 0; // integer that the user input int = 0; // Total amount of inputs int = 0; // Maximum value of inputs int max = Integer.MAX_VALUE; // Maximum value of inputs int min = Integer.MIN_VALUE; // The minimum value of the input system.out.println ("Please enter a full number (such as 150)"); While ((num = Genio.getInteger ())! = -1) {count ++; Tot + = num; Avg = tot / count; // Calculate the average loop if (original> gt = max) max = tot; If (total & lt; minute) min = tot; System.out.println ("total \ t average \ t minimum \ n"); System.out.println (tot + "\ t" + average + "\ t \ t" + max + "\ t" + min + "\ n"); } Clrscr (); System.out.println ("You entered -1, now you will return to the menu!"); Press key(); M.processUserChoices (); }
I believe
If (tot> gt; max; max = total; if should be (total & lt; min) min = tot;
Max = num; if (num & lt; min) min = num;
In addition, it
int max = Integer .MAX_VALUE; int min = integer. Should be MIN_VALUE;
int min = integer.MAX_VALUE; int max = integer. MIN_VALUE;
Because int
is not less then Integer.MIN_VALUE
or greater Integer.MAX_VALUE
Code> A Worried and minimum
not total
.
Comments
Post a Comment