当前位置:网站首页>使用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);
}
}
边栏推荐
- Find the original root
- C language file operation
- A tool for quickly switching local host files -- switchhosts
- Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
- C语言进阶——可存档通讯录(文件)
- 【Day_02 0419】排序子序列
- Leetcode 42. rainwater connection
- 输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
- If I want to listen to Jay Chou with you, I want you to listen to my whole youth
- [MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM
猜你喜欢

Redis sentinel cluster setup

redis 哨兵集群搭建

Cdga | how to build data asset catalogue?

Intelligent fire protection application based on fire GIS system

Mobile web

Using dynamic libraries in VS
![[pytorch] CNN practice - flower species identification](/img/af/81e2735ba385ba3d851e61a5fe2bef.png)
[pytorch] CNN practice - flower species identification

力扣5: 最长回文子串

JVM class loading and GC garbage collection mechanism

Widget is everything, widget introduction
随机推荐
Embedded sharing collection 14
Amd zen4 game God u reached 208mb cache within this year, which is unprecedented
PG Vacuum 杂谈之 auto vacuum
BPG笔记(四)
【Day_06 0423】把字符串转换成整数
VRRP protocol and experimental configuration
【Day_02 0419】倒置字符串
【Day_02 0419】排序子序列
YOLOv6:又快又准的目标检测框架开源啦
BPG notes (IV)
【Day_04 0421】计算糖果
【pytorch】微调技术
[day_050422] statistical palindrome
CV (1)- Introduction
Nuxt configuration topic switching
Do it yourself smart home: intelligent air conditioning control
【Day_05 0422】连续最大和
WebAPI整理
C language explanation series - comprehensive exercises, guessing numbers games
[untitled]