[Java] Method ที่ใช้ใน Class Scanner

ในการใช้งาน class Scanner จะต้องสร้างตัวแปร object มารับข้อมูลที่เกิดจากการทำงานของ method เหล่านั้นด้วยเช่น
  • nextByte() รับข้อมูลเลขจำนวนเต็มชนิด Byte ทางแป้นพิมพ์
  • nextDouble() รับข้อมูลเลขทศนิยมชนิด Double ทางแป้นพิมพ์
  • nextFloat() รับข้อมูลเลขทศนิยมชนิด Float ทางแป้นพิมพ์
  • nextInt() รับข้อมูลเลขจำนวนเต็มชนิด Int ทางแป้นพิมพ์
  • nextLine() รับข้อมูลแบบ String ทางแป้นพิมพ์
  • nextLong() รับข้อมูลตัวเลขชนิด Long ทางแป้นพิมพ์
  • NextShort() รับข้อมูลตัวเลขชนิด Short ทางแป้นพิมพ์
ตัวอย่าง example7.java
***** ปล.ทุกครั้งที่เรียกใช้ Scanner ต้อง import java.util.Scanner; ด้วย *****
import java.util.Scanner;
public class example7 {
    public static void main(String[] args) {
        int x,y;    
  Scanner in  =  new Scanner(System.in); 
  System.out.print("Input Number 1  :  ");
  x = in.nextInt();   
  System.out.print("Input Number 2  :  ");
  y = in.nextInt();   
  System.out.println(x + " + " + y + " = " + (x+y) ); 
    }
}
ผลลัพธ์จากการรันโปรแกรม





ไม่มีความคิดเห็น:

แสดงความคิดเห็น