Package day12_01;
*
* * Guide package; Create object; Invoke Method
* There is a static field under the System class:
* Public static final inputstream in; standard input stream, corresponding to keyboard input
*
* InputStream is=system.in
*
class demo{
* public static final int x=10;
* Public static final Student s=new Student ();
* * } * int y=demo.x;
* Student S=demo.s;
* * * Construction Method:
* Scanner (inputstream source)
/
Import java.util.scanner;//Guide Bag Public
class Scannerdemo {public
static void Main (string[] args) {
//Create object
Scanner sc=new Scanner ( system.in);
int X=sc.nextint ();
System.out.println ("x:" +x);
}
Member Methods
Package day12_02;
Import Java.util.Scanner;
*
* Basic format:
* public boolean hasnextxxx ():
* public Xxx nextxxx ():
*
* Example: int
* Public Boolean has Nextint ()
* public int nextint ()
*
Note
* inputmismatchexception: input and what you want does not match
* * Public Class Scannerdemo {public
static void Main (string[] args) {
//Create object
Scanner sc = new Scanner (system.in);
//Get Data
if (Sc.hasnextint ()) {
int x=sc.nextint ();
System.out.println ("x:" +x);
}
else {
System.out.println ("The data you entered is incorrect");}}
Two commonly used methods
Package day12_03;
Import Java.util.Scanner; * * Common two methods * public int nextint () * Public String nextline () * * Problem: Get a value first, then get a string * main reason: newline symbol \ n * Solve the problem: * A To get a numeric value, then create a new keyboard input object to get the string * B. All the data is followed by the string, then what, the corresponding conversion of what * * public class Scannerdemo {public static void M
Ain (string[] args) {Scanner sc=new Scanner (system.in);
Gets the value//int a=sc.nextint () of two int types;
int B=sc.nextint ();
System.out.println ("A:" +a+ ", B:" +b);
System.out.println ("-------------");
Gets the value//String S1=sc.nextline () of two string classes;
String S2=sc.nextline ();
System.out.println ("S1:" +s1+ ", S2:" +s2);
System.out.println ("-------------");
Gets a string S1=sc.nextline () in the fetch of an int value//string;
int B=sc.nextint (); System.out.println("S1:" +s1+ ", B:" +b);
System.out.println ("------------");
First get an int value, in get a string//int a =sc.nextint ();
String S2=sc.nextline ();
System.out.println ("A:" +a+ ", S2:" +s2);
int A=sc.nextint ();
Scanner sc2=new Scanner (system.in);
String S=sc2.nextline ();
System.out.println ("A:" +a+ ", S:" +s);
}
}