1 //Fig. 2.7:addition2.java, command line Add "Java University Basic Tutorial _ Sixth Edition 43rd page"2 3 PackageFirst ;4 5 ImportJava.util.Scanner;//Program uses class Scanner6 7 Public classAddition2 {8 Public Static voidMain (String args[]) {9Scanner input =NewScanner (system.in);Ten One intNumber1; A intnumber2; - intsum; - theSystem.out.print ("Enter First integer:"); -Number1 =input.nextint (); - -System.out.print ("Enter Second integer:"); +Number2 =input.nextint (); - +sum = Number1 +number2; A atSystem.out.printf ("Sum is%d\n", sum); - - Input.close (); The original book does not have this line, scanner class objects to close after use - } - -}
Eclipse is smart, when there is no 25th line of code, line 9th will prompt: Scanner object needs to be closed, and can bring up the introduction about the Close method
Scanner class (read the command line input number)