java 获取用户输入

/*

如何从键盘获取不同类型的变量:需要使用Scanner类

1.导包:import java.util.Scanner;

2.Scanner的实例化:Scanner scan = new Scanner(System.in);

3.调用Scanner类的相关方法(next()/nextXxx()),来获取指定类型的变量。

import java.util.Scanner;
class ScannerText{
public static void main(String[] args){

Scanner scan = new Scanner(System.in);

System.out.println("please input your name:");
String name = scan.next();
System.out.println(name);

System.out.println("please input your age:");
int age = scan.nextInt();
System.out.println(age);

System.out.println("please input your weight:");
double weight = scan.nextDouble();
System.out.println(weight);

System.out.println("dou u love me?");
boolean isLove = scan.nextBoolean();
System.out.println(isLove);

System.out.println("please input your sex:");
String gender = scan.next();
char genderChar = gender.charAt(0);
System.out.println(genderChar);

}}

 

posted @ 2022-09-19 21:32  emary  阅读(196)  评论(0编辑  收藏  举报