当前位置:网站首页>使用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);
}
}
边栏推荐
- CCTV dialogue ZTE: why must the database be in your own hands?
- [day_070425] legal bracket sequence judgment
- 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
- C语言进阶——可存档通讯录(文件)
- What are the aspects of performance testing? What are the classification and testing methods?
- 【Day_05 0422】连续最大和
- English sentence pattern reference exclusive Edition - attributive clause
- Webapi collation
- Convert amount figures to uppercase
- Interpretation of TPS motion (cvpr2022) video generation paper
猜你喜欢

输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
![[pytorch] fine tuning technology](/img/d3/6d0f60fffd815f520f4b3880bd0ac7.png)
[pytorch] fine tuning technology

Convolutional neural network (IV) - special applications: face recognition and neural style transformation

【无标题】

How can machinery manufacturing enterprises do well in production management with the help of ERP system?

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

Leetcode:940. How many subsequences have different literal values

Interpretation of TPS motion (cvpr2022) video generation paper

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

【Day_06 0423】不要二
随机推荐
【Day_03 0420】字符串中找出连续最长的数字串
nuxt 配置主题切换
Oc/swift Technology Download File (breakpoint continuation AFN download file alamofire Download File native download) (source code)
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
[nanny level] package volume optimization tutorial
【Day02_0419】C语言选择题
English sentence pattern reference exclusive Edition - attributive clause
Distributed | practice: smoothly migrate business from MYCAT to dble
Jz36 binary search tree and bidirectional linked list
C language file operation
力扣5: 最长回文子串
Implementation of PHP multitask second timer
JS date details, string to date
Upgrade appium automation framework to the latest 2.0
Code runner for vs code, with more than 40million downloads! Support more than 50 languages
[day_030420] find the longest consecutive number string in the string
[1] Basic knowledge of mathematical modeling
What is spark serialization for?
带你搞透IO多路复用原理(select、poll和epoll)
[day_040421] calculate candy