当前位置:网站首页>使用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);
}
}
边栏推荐
- MySQL multi table query introduction classic case
- [day_070425] legal bracket sequence judgment
- 【pytorch】图片增广
- [day02_0419] C language multiple choice questions
- Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
- 【Day_03 0420】字符串中找出连续最长的数字串
- [pytorch] CNN practice - flower species identification
- Flex layout
- Oc/swift Technology Download File (breakpoint continuation AFN download file alamofire Download File native download) (source code)
- 【Day04_0421】C语言选择题
猜你喜欢

Interview difficulties: difficulties in implementing distributed session, this is enough!

Yolov6: the fast and accurate target detection framework is open source

Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4

PG vacuum auto vacuum

Code runner for vs code, with more than 40million downloads! Support more than 50 languages

Mobile web

带你搞透IO多路复用原理(select、poll和epoll)

YOLOv6:又快又准的目标检测框架开源啦
![[day_060423] no two](/img/2b/5bcb3e089a3157fe72a50ddb767e63.png)
[day_060423] no two

redis 哨兵集群搭建
随机推荐
Map集合继承结构
C language file operation
【Day_07 0425】合法括号序列判断
C语言进阶——可存档通讯录(文件)
Why use the static keyword when defining methods
【Day_03 0420】数组中出现次数超过一半的数字
VRRP principle and basic commands
【无标题】
将一个正整数分解质因数,要求分解成尽可能小的多个的因数。
【保姆级】包体积优化教程
[1] Basic knowledge of mathematical modeling
[specified interval inversion in BM2 linked list]
[day04_0421] C language multiple choice questions
If I want to listen to Jay Chou with you, I want you to listen to my whole youth
【Day06_0423】C语言选择题
Embedded sharing collection 15
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM
[day_030420] numbers that appear more than half of the time in the array
白盒测试的概念、目的是什么?及主要方法有哪些?
Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!