Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, we aint here to do your homework ;-)</p> <p>second, write first up what you want.</p> <p>for example:</p> <ul> <li>create file</li> <li>write random numbers to file (0-1000) (is it allowed to have a number twice?)</li> <li><p>close/save the file</p></li> <li><p>open the file</p></li> <li>read the file</li> <li>find highest number</li> <li>find lowest number</li> <li>calc average</li> <li>count highest consecutive values</li> </ul> <p>Translate that to logic you understand before trying it in java.</p> <p>step 4: how would you count amount of consecutive numbers on paper? f.e. - every time a new number is one higher then the number before, increase the temp variable. <br> - every time it is not ( new value > old value ) compare the temp variable with highest consecutive variable and save the highest one. reset the temp variable aftewards</p> <p>This would result in:</p> <pre><code>int largestConsecutive = 0; int largestConsecutiveTemp=0; int previewValue =9999; // loop through the lines of the file, each Next elements represents one number. // Use this loop to calc the result values, not to print out on every new number while (infile.hasNext()){ ... if(num &gt; previewValue){ //do something } else { //do something } ... } </code></pre> <p>The while loop is kinda wrong too on finding the smallest/largest numbers:</p> <pre><code>while (infile.hasNext()){ num = infile.nextInt(); largest = infile.nextInt(); smallest = infile.nextInt(); System.out.println(num); if (num &lt; largest){ largest = num; } if (num &gt; smallest){ smallest = num; } System.out.println("The largest is: " + largest); prw.println("The largest is: " + largest); System.out.println("The smallest is: " + smallest); prw.println("The smallest is: " + smallest); System.out.println("The average is: " + average); prw.println("The average is: " + average); } </code></pre> <p>You override every time your smallest/largest variables.</p> <pre><code>largest = 0; // 0, because when you compare with numbers between 0-1000, the new value is larger smallest = 9999; // 0, would be wrong here as it would be abvious the smallest number total = 0; amount=0; // loop through the lines of the file, each Next elements represents one number. // Use this loop to calc the result values, not to print out on every new number while (infile.hasNext()){ num = infile.nextInt(); // override the largest variable with the new largest number if (num &gt; largest){ largest = num; } // override the smallest variable with the new smallest number if (num &lt; smallest){ smallest = num; } // needed to calc the average total += num; amount++; } average = (total / amount); System.out.println("The largest is: " + largest); prw.println("The largest is: " + largest); System.out.println("The smallest is: " + smallest); prw.println("The smallest is: " + smallest); System.out.println("The average is: " + average); prw.println("The average is: " + average); </code></pre>
    singulars
    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.
    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