× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Adding numbers read in a loop

Aug 21, 2016
<p>To add numbers read in a loop, create the variable outside the loop and add to it inside the loop.</p> <p> </p> <p>For instance:</p> <p> </p> <pre>while ( ... ) { int total = 0; // Don't forget to initialize your variable, or it'll be null! while ( ... ) { int sales = in.nextInt(); total += sales; ... // other loop stuff } writer.println ( "\t" + decimalFormat.format((double) total/100)); ... }</pre>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus