当前位置:网站首页>使用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);
}
}
边栏推荐
猜你喜欢

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言

Advanced C language - archived address book (file)

Input the records of 5 students (each record includes student number and grade), form a record array, and then output them in order of grade from high to low The sorting method adopts selective sortin

Convolutional neural network (II) - deep convolutional network: case study

【无标题】

09 eth smart contract
![[pytorch] picture enlargement](/img/79/2eeefcb4623156f65957d78997e138.jpg)
[pytorch] picture enlargement
![[pytorch] CNN practice - flower species identification](/img/af/81e2735ba385ba3d851e61a5fe2bef.png)
[pytorch] CNN practice - flower species identification

【pytorch】CNN实战-花朵种类识别
![[day05_0422] C language multiple choice questions](/img/cb/0019ec819480bd9f52055e726b323a.png)
[day05_0422] C language multiple choice questions
随机推荐
[day_060423] convert string to integer
TPS Motion(CVPR2022)视频生成论文解读
【Day03_0420】C语言选择题
Intelligent fire protection application based on fire GIS system
C language introduction practice (7): switch case calculation of days in the year (normal year / leap year calculation)
力扣——4. 寻找两个正序数组的中位数
移动web
Upgrade appium automation framework to the latest 2.0
【Day_06 0423】不要二
Nuxt configuration topic switching
What is spark serialization for?
How can machinery manufacturing enterprises do well in production management with the help of ERP system?
RNN recurrent neural network
【Day_02 0419】倒置字符串
[untitled]
Flex layout
【Day_06 0423】把字符串转换成整数
A tool for quickly switching local host files -- switchhosts
[day_020419] sort subsequence
[day06_0423] C language multiple choice questions