java Scanner 输入输出总结

本文总结了Java中Scanner类的使用,包括println方法、Scanner的输入处理如nextInt()、nextLine(),以及如何检查是否还有下一个输入。通过示例解释了各方法在处理输入时光标的移动和对空白字符的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
    }
}

在这里插入图片描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值