java - How can I include in my program that the integers entered must be between 0 and 100? -
the exercise asked me calculate average, maximum, , minimum. code fulfills purpose, need include somewhere in code exam grades entered user must between 0 , 100. best way include this? here code.
import java.util.scanner; import java.io.*; import java.text.decimalformat; public class hw { public static void main ( string[] args ) { int maxgrade = integer.min_value; int mingrade = integer.max_value; int count=0; int total=0; final int sentinel = -1; int score; scanner scan = new scanner( system.in); system.out.println( "to calculate class average, enter each test score."); system.out.println( "when finished, enter -1."); system.out.print( "enter first test score > "); score = scan.nextint(); while (score != sentinel ) { total += score; count ++; if( score > maxgrade) maxgrade = score; if( score < mingrade) mingrade = score; system.out.print("enter next test score > "); score = scan.nextint(); } if (count != 0) { decimalformat onedecimalplace = new decimalformat("0.0"); system.out.println( "\nthe class average " + onedecimalplace.format( (double) (total) / count )); system.out.println( "the minimum value " + mingrade); system.out.println( "the maximum value " + maxgrade); } else system.out.println("\nno grades entered"); } }
thank you!
create nested loop stop executing once user has met criteria. this:
while (score < 0 || score > 100) { system.out.println("please enter number between 0 , 100."); while(!scan.hasnextint()) { scan.next(); } score = scan.nextint(); }
note code has not been tested, idea of should like.
Comments
Post a Comment