1 in order to make the program's data more consistent with the development data, we added keyboard entry.
Make the program more flexible.
So, how do we implement keyboard data entry?
a: Guide Package
format:
import java.util.Scanner;
Location:
on class.
b: Create a Keyboard entry object
Format:
scanner sc = new Scanner (system.in) ;
c: Get Data from Objects
Format:
int x = Sc.nextint ();
Import Java.util.Scanner; class Scannerdemo { publicstaticvoid main (string[] args) { // Create a keyboard entry data object new Scanner (system.in); System.out.println ("Please enter a data:"); int x = sc.nextint (); System.out.println ("The data you entered is:" +x);} }
1 /*2 Practice:3 keyboard input Three data, get the maximum value of these three data4 keyboard input Two data to compare whether the two data is equal5 */6 ImportJava.util.Scanner;7 8 classScannerTest3 {9 Public Static voidMain (string[] args) {Ten //keyboard input Three data, get the maximum value of these three data One A //Creating a Keyboard entry object -Scanner sc =NewScanner (system.in); - theSystem.out.println ("Please enter the first data:"); - intA =sc.nextint (); - -System.out.println ("Please enter a second data:"); + intb =sc.nextint (); - +System.out.println ("Please enter a third data:"); A intc =sc.nextint (); at - //get the maximum value in these three data - inttemp = ((a > B)?a:b); - intmax = (temp > c?)temp:c); -System.out.println ("Max:" +max); -System.out.println ("------------------"); in - //keyboard input of two data toSystem.out.println ("Please enter the first data:"); + intx =sc.nextint (); - theSystem.out.println ("Please enter a second data:"); * inty =sc.nextint (); $ Panax Notoginseng //Compare whether the two data are equal - BooleanFlag = (x = =y); theSystem.out.println ("flag:" +flag); + } A}
2
Java Note 7 entry