当前位置:网站首页>使用Scanner从键盘获取多种数据类型
使用Scanner从键盘获取多种数据类型
2022-07-26 06:28:00 【性能测试记录】
如何从键盘获取不同类型的变量,需要使用Scanner类?
具体实现步骤:
1.导包
2.Scanner的实例化
3.调用Scanner类的相关方法,来获取指定类型的变量
获取int数据类型
import java.util.Scanner;
class ScannerTest{
public static void main (String[] args) {
Scanner sc= new Scanner(System.in);
int num=sc.nextInt();
}
}
对于char型的获取,Scanner没有提供相关的方法,只能获取一个字符串.
import java.util.Scanner;
class ScannerTest{
public static void main (String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("请输入你的年龄");
int num=sc.nextInt();
System.out.println(num);
System.out.print("请输入你的姓名");
String name=sc.next();
System.out.println(name);
System.out.print("你是否相中?(true/false)");
boolean isanswer=sc.nextBoolean();
System.out.println(isanswer);
System.out.print("请输入你的性别(男/女)");
String gender=sc.next();
char genderChar=gender.charAt(0);//获取索引为0位置上的字符
System.out.print(genderChar);
}
}
边栏推荐
- 【BM2 链表内指定区间反转】
- Latex merges multiple rows and columns of a table at the same time
- 【Day_04 0421】计算糖果
- 机械制造企业如何借助ERP系统,做好生产管理?
- Jz36 binary search tree and bidirectional linked list
- Leetcode:940. How many subsequences have different literal values
- 移动web
- Convolutional neural network (II) - deep convolutional network: case study
- [day_050422] continuous maximum sum
- 力扣5: 最长回文子串
猜你喜欢

Basis of multimodal semantic segmentation

多目标检测

Embedded sharing collection 14

Leetcode:741. picking cherries

Find the original root

Niuke network: TOPK problem of additive sum between two ordinal groups

VRRP principle and basic commands

Map集合继承结构

TPS Motion(CVPR2022)视频生成论文解读
C language explanation series - comprehensive exercises, guessing numbers games
随机推荐
Introduction of four redis cluster schemes + comparison of advantages and disadvantages
【Day_02 0419】排序子序列
[day_050422] statistical palindrome
Viewing the technology stack of distributed system from the crash report of station B
What is KVM? What is KVM virtual machine?
JVM class loading and GC garbage collection mechanism
Using dynamic libraries in VS
BigDecimal变为负数
Servlet无法直接获取request请求中的JSON格式数据
C语言进阶——可存档通讯录(文件)
Should we test the Dao layer?
英语句式参考纯享版 - 定语从句
CCTV dialogue ZTE: why must the database be in your own hands?
[pytorch] CNN practice - flower species identification
带你搞透IO多路复用原理(select、poll和epoll)
[day_040421] binary conversion
Matlab vector and matrix
Leetcode 347. top k high frequency elements
Leetcode 42. rainwater connection
Interview difficulties: difficulties in implementing distributed session, this is enough!