1. Scanner类使用

 package cn.itcast_01;

 /*
* Scanner:用于接收键盘录入数据。
*
* 前面的时候:
* A:导包
* B:创建对象
* C:调用方法
*
* System类下有一个静态的字段:
* public static final InputStream in; 标准的输入流,对应着键盘录入。
*
* InputStream is = System.in;
*
* class Demo {
* public static final int x = 10;
* public static final Student s = new Student();
* }
* int y = Demo.x;
* Student s = Demo.s;
*
*
* 构造方法:
* Scanner(InputStream source)
*/
import java.util.Scanner; public class ScannerDemo {
public static void main(String[] args) {
// 创建对象
Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.println("x:" + x);
}
}

测试类: 

 package cn.itcast_02;

 import java.util.Scanner;

 /*
* 基本格式:
* public boolean hasNextXxx():判断是否是某种类型的元素
* public Xxx nextXxx():获取该元素
*
* 举例:用int类型的方法举例
* public boolean hasNextInt()
* public int nextInt()
*
* 注意:
* InputMismatchException:输入的和你想要的不匹配
*/
public class ScannerDemo {
public static void main(String[] args) {
// 创建对象
Scanner sc = new Scanner(System.in); // 获取数据
if (sc.hasNextInt()) {
int x = sc.nextInt();
System.out.println("x:" + x);
} else {
System.out.println("你输入的数据有误");
}
}
}

最新文章

  1. C#调用C++代码遇到的问题总结
  2. Python基础-函数篇
  3. Hibernate Tools for Eclips Plugins
  4. Unity3D脚本调用Objective C代码实现游戏内购买
  5. (WCF) WCF and Service Debug
  6. UVALive 3661 Animal Run(最短路解最小割)
  7. samba和squid 安装
  8. 学习笔记——模板模式Template
  9. Visual Studio 实用技能
  10. 安装Mediamanager 后Messenger后无法登录
  11. C# 各类常见Exception 异常信息
  12. 利用cookie存放模糊查询的信息
  13. c++builder 6 [Linker Fatal error] Unable to open file 'PROXIES.OBJ'
  14. 使用原生js实现前端分页功能
  15. docker搭建lnmp(一)
  16. C++中的关键字用法--- explicit
  17. PHP中对用户身份认证实现两种方法
  18. jQuery wordexport导出 word
  19. noip模拟题题解集
  20. 【Best Time to Buy and Sell Stock】cpp

热门文章

  1. 什么是SocLib
  2. 为什么Kafka那么快,明显领先其他mq?
  3. ORACLE--12C--多租户体系架构概念
  4. 【3dsMax安装失败,如何卸载、安装3dMax 2013?】
  5. C++11并发编程:async,future,packaged_task,promise
  6. 4、Angular2 pipe
  7. 在Android源码中如何吧so库打包编译进入apk, 集成第三方库(jar和so库)
  8. android 自定义
  9. 我是一只IT小小鸟读书笔记3
  10. EL表达式的内置对象