Java SE笔记: 用户输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Scanner;
class InputDemo
{
public static void main(String[] args)
{
// 1. 创建Scanner类的对象,这个对象从控制台读取用户的输入的内容
Scanner sc = new Scanner(System.in);
// 读取整数 浮点数 布尔型
// 2. 打印信息,提示一下用户准备输入什么内容
System.out.print("请输入一个数(1-7):");
// 3. 读取用户的输入的内容
int week = sc.nextInt();// nextByte() nextShot() nextLong() nextFloat() nextDouble() nextBoolean()
// 4. 输出一下看看。
System.out.print("用户输入的是"+week);
// 2. 读取字符串
System.out.print("请输入字符传:");
String str = sc.next();
System.out.println("输入的字符串:"+str);
char c = str.charAt(0);
System.out.println("第一个字符是:"+c);
int year,month,day;
System.out.print("请输入年份:");
year = sc.nextInt();
System.out.print("请输入月份:");
month = sc.nextInt();
System.out.print("请输入日期:");
day = sc.nextInt();
System.out.println("用户输入的是"+year+"-"+month+"-"+day);
}
}
文章目录