實現步驟 1.導包:import java.util.Scanner; 2.Scanner類的實例化:Scanner scan = new Scanner(System.in); 3.調用Scanner類的相關方法(next() nextInt())獲取指定類型的變數; 註意:在控制台,如果輸入的類 ...
實現步驟
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