print กับ println การแสดงทางจอภาพ

| ไอที | Java | 28626

การใช้คำสั่ง print กับ println ต่างกันแค่นิดเดียว คือ

print -:- เป็นการแสดงผลออกทางหน้าจอโดยไม่ขึ้นบรรทัดใหม่
println -:- เป็นการแสดงผลออกทางหน้าจอโดยขึ้นบรรทัดใหม่ให้

ตัวอย่างที่ 1

public class Main {
    public static void main(String[] args) {
        System.out.print("Welcome to ");
        System.out.println("www.doesystem.com");
    }
}

Output ที่ได้คือ Welcome to www.doesystem.com

ตัวอย่างที่ 2

public class Main {
    public static void main(String[] args) {
        System.out.print("Welcome to");
        System.out.println("www.doesystem.com");
    }
}

Output ที่ได้คือ Welcome towww.doesystem.com

สรุปผล การใช้คำสั่ง print เป็นการแสดงออกทางจอภาพโดยไม่ขึ้นบรรทัดใหม่ไม่มีช่องว่าง จะเป็นการนำมาต่อกันแบบตรง ๆ เลย ต่อไปเรามาดูคำสั่ง println กันบ้างดีกว่า

ตัวอย่างที่ 3

public class Main {
    public static void main(String[] args) {
        System.out.println("Welcome to");
        System.out.println("www.doesystem.com");
    }
}

Output ที่ได้คือ Welcome to www.doesystem.com

สรุปผล การใช้คำสั่ง println เป็นการแสดงออกทางจอภาพโดยขึ้นบรรทัดใหม่ทันทีดังข้างบน

ตัวอย่างที่ 4

public class Main {
    public static void main(String[] args) {
        System.out.print("Welcome to
");
        System.out.println("www.doesystem.com");
    }
}

Output ที่ได้คือ Welcome to www.doesystem.com

สรุปผล เราสามารถใช้คำสั่ง print แล้วตามด้วย เพื่อบอกว่าให้ขึ้นบรรทัดใหม่ได้

comments




เว็บเพื่อนบ้าน
DoesystemDevcodeMathMySelfHowToClicksBlogJavaExample