"นานมาแล้ว คอมพิวเตอร์แสดงได้เฉพาะข้อความ โปรแกรมแสดงข้อมูลบนหน้าจอหลังจากได้รับอินพุตจากแป้นพิมพ์ สิ่งนี้เรียกว่า 'คอนโซลผู้ใช้อินเทอร์เฟซ' หรือเรียกง่ายๆ ว่า 'คอนโซล' อินเทอร์เฟซหน้าต่างเป็นทางเลือกแทนคอนโซลด้วย อินเทอร์เฟซประเภทนี้ ผู้ใช้โต้ตอบกับโปรแกรมผ่านหน้าต่างอย่างน้อยหนึ่งหน้าต่าง เนื่องจากเราเพิ่งเรียนรู้วิธีการเขียนโปรแกรม เราจะเริ่มด้วยการทำงานกับคอนโซล"

"เอาล่ะ."

"ข้อความแสดงบนคอนโซล (หน้าจอ) ต่อเนื่องกัน บรรทัดต่อบรรทัด ข้อความถูกป้อนโดยใช้แป้นพิมพ์ เพื่อหลีกเลี่ยงข้อผิดพลาด การป้อนข้อมูลด้วยแป้นพิมพ์จะแสดงบนหน้าจอ บางครั้งดูเหมือนว่าผู้ใช้และโปรแกรมกำลังสลับกัน เขียนสิ่งต่าง ๆ บนหน้าจอ "

"คุณสามารถใช้ เมธอด System.out.print () เพื่อแสดงข้อความบนหน้าจอ วิธีนี้เพียงแสดงข้อความ ในขณะที่System.out.println () แสดงข้อความและเลื่อนเคอร์เซอร์ไปที่บรรทัดถัดไป"

รหัส ผลลัพธ์
System.out.print("Rain");
System.out.print("In");
System.out.print("Spain");
เรนอินสเปน
System.out.print("Rain");
System.out.println("In");
System.out.print("Spain");
ฝนตก
ในสเปน
System.out.println("Rain");
System.out.println("In");
System.out.println("Spain");
ฝนตก
ใน
สเปน

"เพื่อให้ข้อความแยกออกจากกัน เราจำเป็นต้องเพิ่มช่องว่าง ตัวอย่างเช่น:"

รหัส ผลลัพธ์
int a = 5, b = 6;
System.out.print(a);
System.out.print(b);
56
int a = 5, b = 6;
System.out.print(" " + a + " " + b);
 5 6
int a = 5, b = 6;
System.out.print("The sum is " + (a + b));
The sum is 11

"เข้าใจแล้ว"

"สิ่งนี้ทำให้คุณสามารถแสดงอะไรก็ได้บนหน้าจอ: อ็อบเจกต์ Java ทั้งหมดสามารถแปลงเป็นสตริงได้ คลาส Java ทั้งหมดมาจากคลาสอ็อบเจกต์ซึ่งมีเมธอด toString() เมธอดนี้เรียกเมื่อคุณต้องการแปลงอ็อบเจกต์เป็น สตริง "

รหัส คำอธิบาย
Cat cat = new Cat("Oscar");
System.out.println("The cat is " + cat);
ตัวอย่างทั้งสามนี้เทียบเท่ากัน
Cat cat = new Cat("Oscar");
System.out.println("The cat is " + cat.toString());
Cat cat = new Cat("Oscar");
String catText = cat.toString();
System.out.println("The cat is " + catText);
3
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Display right away
Programming isn't always difficult. Sometimes you need to do simple tasks. And the more similar tasks you have to do, the more you want to automate them. Let's implement one more method. Ultimately, objects without methods don't do anything. We'll pass the string s to the method, which will then display it on the screen.

"แต่โปรแกรมของฉันแสดง ' The cat is com.codegym.lesson3.Cat@1fb8ee3 ' หมายความว่ายังไงในโลกนี้"

"เมธอด toString() มาตรฐานของคลาสอ็อบเจกต์ส่งคืนสตริงที่ประกอบด้วยชื่อคลาสและที่อยู่หน่วยความจำของอ็อบเจ็กต์ (ในรูปแบบเลขฐานสิบหก)"

"เอ่อ-ฮะ แล้ววิธีนี้จะมีประโยชน์อะไรล่ะ"

"คุณสามารถเขียนการใช้งาน toString() ของคุณเองในชั้นเรียนของคุณ จากนั้นจึงเรียกเมธอดนั้น"

"จริงเหรอ?

"นี่คืองานบางส่วนจากดิเอโก"

3
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Currency exchange
Loan sharking, sales, banking: do you have a backup plan if programming doesn't work out? No? In that case, let's help traders and financiers with our top-notch programs. First, we'll organize a currency exchange: write a program that converts euros to dollars at a given exchange rate.
3
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Task with percentages
If you weren't afraid of problems with percentages in school, then this problem won't scare you either. But if you do suddenly find yourself afraid, it's time to face your fear and realize that this is actually a simple and pleasant topic. Let's implement a method that increases the passed integer by 10 percent.
1
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).