实现步骤
1.导包:import java.util.Scanner;
2.Scanner类的实例化:Scanner scan = new Scanner(System.in);
3.调用Scanner类的相关方法(next() nextInt())获取指定类型的变量;
注意:在控制台,如果输入的类型与要求的类型不匹配时,会报异常:InputMisMatchException
代码:
import java.util.Scanner; /** * @author gcstart * @create 2020-03-27 18:54 */ public class test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入你的姓名:"); String name = scanner.next(); System.out.println(name); System.out.println("请输入你的年龄:"); int age = scanner.nextInt(); System.out.println(age); System.out.println("是否相中我了呢?"); boolean isLove = scanner.nextBoolean(); System.out.println(isLove); System.out.println("请输入你的性别(男/女):"); String gender = scanner.next(); char charGender = gender.charAt(0); System.out.println(charGender); } }
作者:Java之美
日期:2020-03-28
来源:https://www.cnblogs.com/897463196-a/p/12585413.html