•     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的配置、监控与优化,比较实用,值得学习。

Java基础 Scanner 使用nextInt接收整数的更多相关文章

  1. Java基础 Scanner 使用nextLine接收字符串

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

  2. 【Java】Scanner类nextInt后使用nextLine无法读取输入

    首先,我们先介绍一下,在学习Java语言,对于字符串的输入,由于Scanner.next()函数无法输入空格及回车,此时,我们就必须要用Scanner.nextLine()解决这类问题, 在使用过程中 ...

  3. java基础——Scanner的基础和进阶

    Scanner对象 目的:用来获取用户的输入 基本语法: Scanner s = new scanner (System.in); 通过Scanner 类的next()和nextLine()方法,获取 ...

  4. 解决一个无聊的问题,如何处理Java用户在dos被收集信息时拷贝带换行符的文本信息造成的while的多次循环(java解决Scanner.next在接收用户输入时出现多个换行的形况)[解决方案一]

    问题描述: 用户在dos窗口输入的时候(web项目不会出现这样的问题,所以这个问题日常碰不到),摁下回车时,Scanner对象的next()扫描用户输入的文本,后面就可以根据输入的字符串进行判断,并执 ...

  5. java中Scanner类nextInt之后用nextLine无法读取输入

    http://blog.csdn.net/wjy1090233191/article/details/42080029 这篇文章写得非常详细和准确

  6. 当Editplus遇到Java的Scanner

    学习Java编程时,我想让变量的值从键盘输入接收进来.平时在dos中运行效果很直观,那么我在Editplus这款开发工具中也可以输入,Editplus是带有控制台.当你运行Java程序时,此时出现的编 ...

  7. Java基础复习3

    循环的嵌套 public class demo8 {     public static void main(String[] args) {         /*  输出########       ...

  8. java基础 File与递归练习 使用文件过滤器筛选将指定文件夹下的小于200K的小文件获取并打印按层次打印(包括所有子文件夹的文件) 多层文件夹情况统计文件和文件夹的数量 统计已知类型的数量 未知类型的数量

    package com.swift.kuozhan; import java.io.File; import java.io.FileFilter; /*使用文件过滤器筛选将指定文件夹下的小于200K ...

  9. Java基础(Scanner、Random、流程控制语句)

    第3天 Java基础语法 今日内容介绍 u 引用数据数据类型(Scanner.Random) u 流程控制语句(if.for.while.dowhile.break.continue) 第1章 引用数 ...

随机推荐

  1. Springboot中自已测试

    签到的测试,需要传入日期,签到7天可获得更多的积分, 构造7天前的签到记录,重写签到方法,进行构造数据 import cn.com.acxiom.coty.api.ws.bean.dto.PointD ...

  2. 运行Vue项目,没办法自动打开浏览器,提示“Unable to open browser. If you are running in a headless environment, please do not use the open flag.”

    留坑,待解决 Unable to open browser. If you are running in a headless environment, please do not use the o ...

  3. LG4723 【模板】常系数线性递推

    P4723 [模板]常系数齐次线性递推 题目描述 求一个满足$k$阶齐次线性递推数列${a_i}$的第$n$项. 即:$a_n=\sum\limits_{i=1}^{k}f_i \times a_{n ...

  4. Bias vs. Variance(2)--regularization and bias/variance,如何选择合适的regularization parameter λ(model selection)

    Linear regression with regularization 当我们的λ很大时,hθ(x)≍θ0,是一条直线,会出现underfit:当我们的λ很小时(=0时),即相当于没有做regul ...

  5. SQL Server Default Trace查看是谁对数据库进行了DDL操作

    在我们的工作中可能会遇到这样一种情形.由于数据库中某些对象被altered/created/deleted,造成我们的应用程序crash. 当我们把问题解决之后,老板可能会问发生了什么?为什么会这样? ...

  6. 对日开发中 PG , PL , SE , PM 是什么

    PG(ProGramer)指程序员. 这类人才在企业中所占数量最多,通常占到整个项目员工数的70%,也是企业中最紧缺的一类职位,一般为具有专业知识的软件工程技术人员. PL(project leade ...

  7. easyui181版本使用记录

    easyui181版本下载地址 简单java+easyui181版本demo下载 注意前端页面的ajax请求路径对应后端 如果再加强样式可使用adminLTE

  8. SpreadJS 复制行

    参考:https://www.cnblogs.com/yeyuqian/p/10750441.html 核心代码: //例子:复制第一行(10列) 复制到 第二行var fromRanges = ne ...

  9. golang-笔记1

    指针: 指针就是地址. 指针变量就是存储地址的变量. *p : 解引用.间接引用. 栈帧: 用来给函数运行提供内存空间. 取内存于 stack 上. 当函数调用时,产生栈帧.函数调用结束,释放栈帧. ...

  10. 服务器上build.xml文件乱码解决(亲测有效)

    前提条件:必须root账户登录系统,否则无权限 1. 修改/etc/sysconfig/i18n: 拷贝如下内容到文件中 #LANG="zh_CN.UTF-8" LANG=&quo ...