一、扫描控制台输入
当通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,,,,,,,直到敲回车键结束,把所输入的内容传给Scanner,作为扫描对象。
如果要获取输入的内容,则只需要调用Scanner的nextLine()方法即可。
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
while (true) {
String line = sc.nextLine();
if (line.equals("exit")) break;
System.out.println(">>>" + line);
}
}
}
public class TestScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
while (true) {
String line = sc.nextLine();
if (line.equals("exit")) break;
System.out.println(">>>" + line);
}
}
}
请输入字符串:
234
>>>234
wer
>>>wer
bye
>>>bye
exit
234
>>>234
wer
>>>wer
bye
>>>bye
exit
Process finished with exit code 0