Scanner Class Profile
A scanner can resolve simple text strings and basic types. Scanner class features: keyboard input data can be achieved, to program them.
Reference types using the steps
1: package guide
Using the import keyword leader packet, until all code packet type conductivity, the introduction of the type of format to be used:
note:
- If you need to use the target class, and the current class in the same package, the package may be omitted guide statements are not written.
- The only java.lang package contents do not need to guide package, other packages require an import statement.
2: Create Object
Using the constructor method of the class, create an object class. format:
3: Call
- Call to a member method of the class, complete the assigned functions. Format: variable name method name ();.
- Call member variables of the class, gets the specified value. Format: variable name member variable name;
Scanner using the steps
View class
- java.util.Scanner: after introduction of such use need to import.
View constructor
- public Scanner (InputStream source): Constructs a new Scanner, produces values specified from the input stream is scanned. Parameter input from the keyboard as the representative System.in
View member method
- public int nextInt (): the input of the next scanning information marker as an int value. (Int get a numeric keyboard input)
- public String next (): the input of the next scan marker information to a String. (String get a numeric keyboard input)
demand
Int three digital keyboard input, then the highest value is obtained.
analysis:
1: Since it is a keyboard input, definitely you need to use the Scanner
2.:Scanner three steps: guide package, create, use nextInt () method
3: Since it is three digits, then called three times nextInt () method to obtain a three int variable
4: who can not simultaneously determine the maximum three digits, to be converted into two steps:
4.1 first determine who was the greatest among the first two, the first two to get the maximum value of
a maximum of 4.2 took the first two, and then the third number is obtained by comparing the maximum value from among the three numbers
5: final results of printing
Code
public class Demo03ScannerMax { public static void main (String [] args) { // create Scanner objects Note: Representative System.in input from the keyboard Scanner SC = new new Scanner (System.in); // Get int numeric keyboard input System .out.println ( "Please enter the first digit:" ); int a = sc.nextInt (); System.out.println ( "Please enter the second number:" ); int B = sc.nextInt () ; System.out.println ( "Please enter the third number:" ); int C = sc.nextInt (); // first among the first two digits give the maximum int? TEMP = A> B A: B; int max = TEMP> C? TEMP: C; System.out.println ( "maximum is:" + max); } }
Results of the