当前位置: 代码迷 >> 综合 >> java Scanner 输入输出总结
  详细解决方案

java Scanner 输入输出总结

热度:111   发布时间:2023-09-22 09:26:43.0

java 输入输出总结

println

System.out.println源码:

/*** Prints a String and then terminate the line.  This method behaves as* though it invokes {@link #print(String)} and then* {@link #println()}.** @param x  The {@code String} to be printed.*/public void println(String x) {if (getClass() == PrintStream.class) {writeln(String.valueOf(x));} else {synchronized (this) {print(x);newLine();//输出后面额外会加一个换行;}}}

Scanner

包名:import java.util.Scanner;
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

next()

Finds and returns the next complete token from this scanner.//会将光标定位到该字符串之后,分隔符(如:空格)前

nextInt()

Scans the next token of the input as an int.//会将光标定位到该数字之后,分隔符(如:空格)前

nextLine()

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. ***The position is set to the beginning of the next line.***(会吃掉当前行的空格)Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

hasNext()

Returns true if the next token matches the pattern constructed from the specified string.

example

import java.util.Scanner;public class ScannerTest {public void ScannerTest1(){Scanner input = new Scanner(System.in);System.out.println("请输入数字:");int option = input.nextInt();//read numerical value from input;System.out.println(option);System.out.println("请输入字符串1:");String string1 = input.nextLine();//read 1st string (this is skipped)System.out.println(string1);System.out.println("请输入字符串2:");String string2 = input.nextLine();//read 2nd string (this appears right after reading numerical value)System.out.println(string2);}public void ScannerTest2(){Scanner in = new Scanner(System.in);//输入并输出一个整数int n = in.nextInt();System.out.println("n:");System.out.println(n);//输入并输出一个字符串String s = in.next();System.out.println("s:");System.out.println(s);//输入并输出一个字符串String ss = in.nextLine();System.out.println("ss:");System.out.println(ss);}public void ScannerTest3(){Scanner in = new Scanner(System.in);//输入并输出一个整数int n = in.nextInt();System.out.println(n);in.nextLine();//添加nextLine吸收回车//输入并输出一个字符串String String1 = in.nextLine();System.out.println(String1);//输入并输出一个字符串String s = in.next();System.out.println(s);//输入并输出一个字符串String String2 = in.next();//改用next()System.out.println(String2);}public void ScannerTest4(){Scanner in=new Scanner(System.in);while(in.hasNext()){String n=in.next();System.out.println(n);}}public static void main(String[] args) {ScannerTest scannerTest = new ScannerTest();System.out.println("ScannerTest1:");scannerTest.ScannerTest1();System.out.println("ScannerTest2:");scannerTest.ScannerTest2();System.out.println("ScannerTest3:");scannerTest.ScannerTest3();System.out.println("ScannerTest4:");scannerTest.ScannerTest4();}
}

java Scanner 输入输出总结

在ScannerTest1中:
java Scanner 输入输出总结
这里多一行空行的原因是println打印string2后将光标定位到下一行,然后没有其它输入,“Process finished with exit code 0”会在下一行显示(因为Process前有一个’\n’)

  相关解决方案