Android(java)学习笔记36:Scanner类使用
2024-08-26 09:26:28
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("你输入的数据有误");
}
}
}
最新文章
- C#调用C++代码遇到的问题总结
- Python基础-函数篇
- Hibernate Tools for Eclips Plugins
- Unity3D脚本调用Objective C代码实现游戏内购买
- (WCF) WCF and Service Debug
- UVALive 3661 Animal Run(最短路解最小割)
- samba和squid 安装
- 学习笔记——模板模式Template
- Visual Studio 实用技能
- 安装Mediamanager 后Messenger后无法登录
- C# 各类常见Exception 异常信息
- 利用cookie存放模糊查询的信息
- c++builder 6 [Linker Fatal error] Unable to open file 'PROXIES.OBJ'
- 使用原生js实现前端分页功能
- docker搭建lnmp(一)
- C++中的关键字用法--- explicit
- PHP中对用户身份认证实现两种方法
- jQuery wordexport导出 word
- noip模拟题题解集
- 【Best Time to Buy and Sell Stock】cpp