Java基础 Scanner 使用nextInt接收整数

  •     JDK :OpenJDK-11
  •      OS :CentOS 7.6.1810
  •      IDE :Eclipse 2019‑03
  • typesetting :Markdown

code

package per.jizuiku.base;

import java.util.Scanner;

/**
 * @author 给最苦
 * @date 2019/06/29
 * @blog www.cnblogs.com/jizuiku
 */
class Demo {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个数据");
        int x = sc.nextInt();
        System.out.println("你所输入的数据是:" + x);

        sc.close();
    }
}

result

请输入一个数据
55
你所输入的数据是:55

sourceCode

/**
    * Constructs a new {@code Scanner} that produces values scanned
    * from the specified input stream. Bytes from the stream are converted
    * into characters using the underlying platform's
    * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
    *
    * @param  source An input stream to be scanned
    */
public Scanner(InputStream source) {
    this(new InputStreamReader(source), WHITESPACE_PATTERN);
}
/**
    * Scans the next token of the input as an {@code int}.
    *
    * <p> An invocation of this method of the form
    * {@code nextInt()} behaves in exactly the same way as the
    * invocation {@code nextInt(radix)}, where {@code radix}
    * is the default radix of this scanner.
    *
    * @return the {@code int} scanned from the input
    * @throws InputMismatchException
    *         if the next token does not match the <i>Integer</i>
    *         regular expression, or is out of range
    * @throws NoSuchElementException if input is exhausted
    * @throws IllegalStateException if this scanner is closed
    */
public int nextInt() {
    return nextInt(defaultRadix);
}
/**
    * Scans the next token of the input as an {@code int}.
    * This method will throw {@code InputMismatchException}
    * if the next token cannot be translated into a valid int value as
    * described below. If the translation is successful, the scanner advances
    * past the input that matched.
    *
    * <p> If the next token matches the <a
    * href="#Integer-regex"><i>Integer</i></a> regular expression defined
    * above then the token is converted into an {@code int} value as if by
    * removing all locale specific prefixes, group separators, and locale
    * specific suffixes, then mapping non-ASCII digits into ASCII
    * digits via {@link Character#digit Character.digit}, prepending a
    * negative sign (-) if the locale specific negative prefixes and suffixes
    * were present, and passing the resulting string to
    * {@link Integer#parseInt(String, int) Integer.parseInt} with the
    * specified radix.
    *
    * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
    * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
    * {@code IllegalArgumentException} is thrown.
    *
    * @param radix the radix used to interpret the token as an int value
    * @return the {@code int} scanned from the input
    * @throws InputMismatchException
    *         if the next token does not match the <i>Integer</i>
    *         regular expression, or is out of range
    * @throws NoSuchElementException if input is exhausted
    * @throws IllegalStateException if this scanner is closed
    * @throws IllegalArgumentException if the radix is out of range
    */
public int nextInt(int radix) {
    // Check cached result
    if ((typeCache != null) && (typeCache instanceof Integer)
        && this.radix == radix) {
        int val = ((Integer)typeCache).intValue();
        useTypeCache();
        return val;
    }
    setRadix(radix);
    clearCaches();
    // Search for next int
    try {
        String s = next(integerPattern());
        if (matcher.group(SIMPLE_GROUP_INDEX) == null)
            s = processIntegerToken(s);
        return Integer.parseInt(s, radix);
    } catch (NumberFormatException nfe) {
        position = matcher.start(); // don't skip bad token
        throw new InputMismatchException(nfe.getMessage());
    }
}

resource

  • [ JDK ] openjdk.java.net
  • [ doc - 参考 ] docs.oracle.com/en/java/javase/11
  • [ 规范 - 推荐 ] yq.aliyun.com/articles/69327
  • [ 规范 - 推荐 ] google.github.io/styleguide
  • [ 源码 ] hg.openjdk.java.net
  • [ OS ] www.centos.org
  • [ IDE ] www.eclipse.org/downloads/packages
  • [ 平台 ] www.cnblogs.com


感谢帮助过 给最苦 的人们。
Java、Groovy和Scala等基于JVM的语言,优秀,值得学习。
规范的命名和代码格式等,有助于沟通和理解。
JVM的配置、监控与优化,比较实用,值得学习。

转载于:https://www.cnblogs.com/jizuiku/p/11107751.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值