1. Enter:
Format 1:scanner sc = new Scanner (new Bufferedinputstream (system.in));
Format 2:scanner sc = new Scanner (system.in);
In the case of a large amount of read data, format 1 will be faster.
Read an integer: int n = sc.nextint (); Equivalent to scanf ("%d", &n); or CIN >> N;
Read a string: string s = Sc.next (); Equivalent to scanf ("%s", s); or CIN >> s;
Read a floating-point number: Double t = sc.nextdouble (); Equivalent to scanf ("%lf", &t); or cin >> T;
Read a whole line: String s = sc.nextline (); Equivalent to gets (s); or Cin.getline (...);
Determine if there is a next input that can be sc.hasnext () or sc.hasnextint () or sc.hasnextdouble () or Sc.hasnextline ()
Public Static voidMain (string[] args) {NumberFormat formatter=NewDecimalFormat ("000000"); String s= Formatter.format (-1234.567);//-001235System.out.println (s); Formatter=NewDecimalFormat ("# #"); S= Formatter.format (-1234.567);//-1235System.out.println (s); S= Formatter.format (0);//0System.out.println (s); Formatter=NewDecimalFormat ("# #00"); S= Formatter.format (0);//xxSystem.out.println (s); Formatter=NewDecimalFormat (". 00"); S= Formatter.format (-.567);//-.57System.out.println (s); Formatter=NewDecimalFormat ("0.00"); S= Formatter.format (-.567);//-0.57System.out.println (s); Formatter=NewDecimalFormat ("#.#"); S= Formatter.format (-1234.567);//-1234.6System.out.println (s); Formatter=NewDecimalFormat ("#.######"); S= Formatter.format (-1234.567);//-1234.567System.out.println (s); Formatter=NewDecimalFormat (". ######"); S= Formatter.format (-1234.567);//-1234.567System.out.println (s); Formatter=NewDecimalFormat ("#.000000"); S= Formatter.format (-1234.567);//-1234.567000System.out.println (s); Formatter=NewDecimalFormat ("#,###,###"); S= Formatter.format (-1234.567);//-1,235System.out.println (s); S= Formatter.format (-1234567.890);//-1,234,568System.out.println (s); //The ; Symbol is used to specify a alternate pattern for negative valuesFormatter =NewDecimalFormat ("#;(#)"); S= Formatter.format (-1234.567);//(1235)System.out.println (s); //The ' symbol is used to quote literal symbolsFormatter =NewDecimalFormat ("' # ' #"); S= Formatter.format (-1234.567);//-#1235System.out.println (s); Formatter=NewDecimalFormat ("' ABC ' #"); S= Formatter.format (-1234.567);//-ABC 1235System.out.println (s); Formatter=NewDecimalFormat ("#.##%"); S= Formatter.format (-12.5678987); System.out.println (s); }
Java input and output