damn...
kind of simple code, but today i just know...
Fig. 13.9 Checking with assert that a value is within range. |
|
1 2 3 import java.util.Scanner; 4 5 public class AssertTest 6 { 7 public static void main( String args[] ) 8 { 9 Scanner input = new Scanner( System.in ); 10 11 System.out.print( "Enter a number between 0 and 10: " ); 12 int number = input.nextInt(); 13 14 15 assert ( number >= 0 && number <= 10 ) : "bad number: " + number; 16 17 System.out.printf( "You entered %d\n", number ); 18 } 19 }
Enter a number between 0 and 10: 5 You entered 5
|
Enter a number between 0 and 10: 50 Exception in thread "main" java.lang.AssertionError: bad number: 50 at AssertTest.main(AssertTest.java:15)
|
|