java - Read a list of nonnegative integers and to display the largest integer, the smallest integer and the average of all the integers -


i have encountered problems calculating of largest , smallest number... if first number entered larger number 2nd number input, not record 1st number largest...

take @ output, elaborate better.. calculation error.. & 1st input problem.. codes below!

public static void main(string[] args) {      int smallest = integer.max_value;     int largest = 0;     int number;     double totalavg = 0;     double totalsum = 0;     int count = 0;      scanner kb = new scanner(system.in);      system.out.println("enter few integers (enter negative numbers end input) :");     while (true) { //loop till user enter "-1"         number = kb.nextint();          //condition loop break         if (number <= -1) {             system.out.println("end of input");             break;         } else {             count = count + 1;         }          if (number < smallest) { //problem 1 : if 1st input num bigger 2nd input num,             smallest = number;  // largest num not recorded..         } else {             largest = number;         }          totalsum = totalsum + number;         totalavg = (totalsum / count);      }      system.out.println("the smallest number have entered : " + smallest);     system.out.println("the largest number have entered : " + largest);     system.out.println("the total sum : " + totalsum);     system.out.println("the total average : " + totalavg);     system.out.println("count : " + count); } // psvm 

do this:

public static void main(string[] args) {  int smallest = integer.max_value; int largest = 0; int number; double totalavg = 0; double totalsum = 0; int count = 0;  scanner kb = new scanner(system.in);  system.out.println("enter few integers (enter negative numbers end input) :"); while (true) { //loop till user enter "-1"     number = kb.nextint();      //condition loop break     if (number <= -1) {         system.out.println("end of input");         break;     } else {         count = count + 1;     }      if (number < smallest) { //problem 1 : if 1st input num bigger 2nd input num,         smallest = number;  // largest num not recorded..     }      //removed else added if      if (number > largest){         largest = number;     }      totalsum = totalsum + number;     totalavg = (totalsum / count);  }  system.out.println("the smallest number have entered : " + smallest); system.out.println("the largest number have entered : " + largest); system.out.println("the total sum : " + totalsum); system.out.println("the total average : " + totalavg); system.out.println("count : " + count); } // psvm 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -