1. 程式人生 > >java用Scanner鍵盤鍵入

java用Scanner鍵盤鍵入

import java.util.*;
public class Paixu {

	public static void main(String[] args) {
		
		 Scanner sc = null;
	        try {
	             sc = new Scanner(System.in); 
	             System.out.println("請輸入您的姓名:"); 
	             String name = sc.nextLine(); 
	             System.out.println("請輸入您的年齡:"); 
	             int age = sc.nextInt(); 
	             System.out.println("請輸入您的體重(kg):"); 
	             float salary = sc.nextFloat(); 
	             System.out.println("您的個人資訊如下:"); 
	             System.out.println("姓名:"+name+"\n"+"年齡:"+age+"\n"+"體重:"+salary); 
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            if(sc != null){
	                sc.close();
	                sc = null;
	            }
	        }
	}
}