Note that there are some explanatory texts on larger screens.

plurals
  1. POJava error in calculation when comparing largest and smallest numbers & sum with while loop
    primarykey
    data
    text
    <p>Here is my assignment:</p> <p>Write a program to read a list of nonnegative integers and to display the largest integer, the smallest integer, and the average of all the integers. The user indicates the end of the input by entering a negative sentinel value that is not used in finding the largest, smallest, and average values. The average should be a value of type <code>double</code> so it will be computed with a fractional part.</p> <p>I've gotten different parts to work with different methods: Method A makes the maximum and minimum correct and the sum wrong and Method B makes the sum and maximum correct and the minimum wrong. The following code demonstrates Method B. Some variables are commented out:</p> <pre><code> public class testthis2 { public static void main(String[] args) { System.out.println("Enter Numbers of Nonnegative Integers."); System.out.println("When complete, enter -1 to end input values."); Scanner keyboard = new Scanner(System.in); //int max = keyboard.nextInt(); int max = 0; int min = max; //The max and min so far are the first score. //int next = keyboard.nextInt(); int count = 0; int sum = 0; boolean areMore = true; boolean run_it = false; //run it if its true //if (max &lt;= -1) { // System.out.println("Thanks for playing!"); // run_it = false; //} // else // run_it = true; while(areMore) //always true { int next = keyboard.nextInt(); //int max = 0; // min = max; if (next &lt; 0) { //if -1 is entered end loop. areMore = false; run_it = false; break; } else //run this badboy if(next &gt;= max) max = next; else if(next &lt;= min) min = next; run_it = true; sum += next; count++; } if (run_it = true) System.out.println("The highest score is " + max); System.out.println("The lowest score is " + min); System.out.println("count " + count); System.out.println("sum " + sum); System.out.println("average " + (double)(sum/count)); System.out.println("Thanks for playing!"); } } </code></pre> <p>When I run this test, the maximum, sum, count, and average are all correct. However, the minimum is wrong, because 0 was clearly not entered. Here's an example test-run: </p> <pre><code>When complete, enter -1 to end input values. 37 25 30 20 11 14 -1 The highest score is 37 The lowest score is 0 count 6 sum 137 average 22.0 Thanks for playing! </code></pre> <p>Any help would be greatly appreciated. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload