การต่อข้อมูล

มีอยู่

"ฉันอยากจะบอกวิธีรวมสตริงกระบวนการรวมหรือรวมสตริงมักเรียกโดยใช้คำสั้นๆ ว่า 'การเรียงต่อกัน'คนรักแมวจะจำได้ง่าย: con-Cat-en-Nation ฉัน ล้อเล่นนะครับ"

"กฎสำหรับการรวมสตริงนั้นง่ายมาก หากเรา 'เพิ่ม' (+) สตริงและอย่างอื่น 'อย่างอื่น' จะถูกแปลงเป็นสตริงโดยปริยายผ่านเมธอด toString ( ) "

“เมื่อกี้คุณคุยกับฉันหรือเปล่า”

"โอเค ฉันจะอธิบายให้ง่ายขึ้น ถ้าเราเพิ่มสตริง ตัวเลขและแมว ทั้งตัวเลขและแมวจะถูกแปลงเป็นสตริง นี่คือตัวอย่างบางส่วน:"

รหัส รหัสเทียบเท่า
Cat cat = new Cat();
String text = "The cat is " + cat;
Cat cat = new Cat();
String s = cat.toString();
String text = "The cat is " + s;
int a = 5;
String text = "a is " + a;
int a = 5;
String s = Integer.toString(a);
String text = "a is " + s;
int a = 5;
String text = a + "a is ";
int a = 5;
String s = Integer.toString(a);
String text = s + "a is ";
Cat cat = new Cat();
int a = 5;
String text = "The cat is " + cat + a;
Cat cat = new Cat();
String s1 = cat.toString();
String s2 = Integer.toString(a);
String text = "The cat is " + s1 + s2;
Cat cat = new Cat();
int a = 5;
String text = a + "The cat is " + cat + a;
Cat cat = new Cat();
String s1 = cat.toString();
String s2 = Integer.toString(a);
String s3 = Integer.toString(a);
String text = s3 + "The cat is " + s1 + s2;
Cat cat = new Cat();
int a = 5;
String text = cat + a + "The cat is " + cat + a;
โปรแกรมไม่คอมไพล์!
การบวกจะดำเนินการจากซ้ายไปขวา ดังนั้นเราจึงได้รับ:ถ้าเราเพิ่ม cat ให้กับตัวเลข จะไม่มีการแปลงสตริงโดยอัตโนมัติ
String text = (((cat + a) + "The cat is ") + cat) + a;
// But you can do this:
Cat cat = new Cat();
int a = 5;
String text = cat + (a + "The cat is ") + cat + a;

// This is the same as:
Cat cat = new Cat();
int a = 5;
String text = ((cat + (a + "The cat is ")) + cat)+a;
Cat cat = new Cat();
String s1 = cat.toString();
String s2 = cat.toString();
String s3 = Integer.toString(a);
String s4 = Integer.toString(a);
String text = s1 + s3 + "The cat is " + s2 + s4;

"ถึงเวลาที่ต้องทำงานสองสามอย่างจากดิเอโก้"

1
งาน
Java ไวยากรณ์,  ระดับบทเรียน
ล็อค
Printing strings
I foresee a recurring need to print strings in the life of a programmer! To print a string, you have to use a specific method... or write your own that has some special features. This task is different in that our method shouldn't simply display a string. Instead, it should change the string by adding the word "printing".
3
งาน
Java ไวยากรณ์,  ระดับบทเรียน
ล็อค
Time conversion
Hours are few, but seconds are many. Perhaps measuring time in seconds would give the illusion that we have more time than we really do? Anyway, enough philosophizing. Let's work on programming. We need to implement a method that will convert hours to seconds, and then we'll see what this gets us.
1
งาน
Java ไวยากรณ์,  ระดับบทเรียน
ล็อค
Fill a pool with water
Today our task is to fill a pool with water. In doing so, we mustn't drown anyone, but we also don't want to hold back: we'll fill it to the brim! The filling method will take the dimensions of the pool. We will assume that it is a parallelepiped, i.e. it has a well-defined length, width and depth. And the method will return the required amount of water. In liters.
ความคิดเห็น
  • เป็นที่นิยม
  • ใหม่
  • เก่า
คุณต้องลงชื่อเข้าใช้เพื่อแสดงความคิดเห็น
หน้านี้ยังไม่มีความคิดเห็นใด ๆ