Last active
October 16, 2022 16:32
-
-
Save Phonbopit/5821454 to your computer and use it in GitHub Desktop.
โจทย์ตัวอย่าง Loop ใน Java รูปแบบต่างๆ
@see http://devsharing.com/2012/java/java-loop-example2/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoopExample6 { | |
public static void main(String[] args) { | |
for(int i=1; i<=5; i++){ | |
for(int j=1; j<=(5-i); j++){ | |
System.out.print(" "); | |
} | |
for(int k=1; k<i; k++){ | |
System.out.print("* "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoopStarExample1 { | |
public static void main(String[] args) { | |
int num = 5; | |
for(int i = 1; i < = num; i++) { | |
/* วนลูปตามจำนวน i บรรทัดแรก พิมพ์ * 1ตัว | |
* เมื่อวนลูปรอบต่อไป i เพิ่ม ก็จะวน2รอบ ไปเรื่อยๆจนครบ 5 | |
*/ | |
for(int j = 1; j <= i; j++) { | |
System.out.print("*"); | |
} | |
System.out.println(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoopStarExample2 { | |
public static void main(String[] args) { | |
int num = 5; | |
for(int i = 1; i < = num; i++) { | |
/* วนลูปตามจำนวน n บรรทัดแรก พิมพ์ * เท่ากับจำนวน n | |
* เมื่อวนลูปรอบต่อไปค่า j จะลด ก็จะวน2รอบ *ก็จะลดไป 1 เรื่อยๆทุกบรรทัด | |
*/ | |
for(int j = num; j >= i; j--) { //สังเกต ลูปนี้ต่างจาก ตัวอย่างแรกนิดนึง | |
System.out.print("*"); | |
} | |
System.out.println(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoopStarExample3 { | |
public static void main(String[] args) { | |
// ลูปนี้ วนตามจำนวนความสูงของพีระมิด | |
for (int i = 5; i > 0; i--) { | |
//ลูปนี้ จะทำการปริ้นช่องว่างออกมาก่อนสร้างพีระมิด | |
//ลองเปลี่ยน print(" ") เป็น print("5"); ถ้ายังไม่เข้าใจ | |
for (int j = i; j > 1; j--) { | |
System.out.print(" "); | |
} | |
//ลูปนี้ จะทำการปริ้น* ซีกซ้ายออกมา | |
//ถ้าไม่เข้าใจ ลองเปลี่ยนจาก * เป็นตัวเลข เพื่อให้เห็นภาพ | |
for (int j = i; j < 5; j++) { | |
System.out.print("*"); | |
} | |
//ลูปนี้ จะทำการปริ้น* ซีกขวา | |
//ถ้าไม่เข้าใจ ลองเปลี่ยนจาก * เป็นตัวเลข เพื่อให้เห็นภาพ | |
for (int j = i; j <= 5; j++) { | |
System.out.print("*"); | |
} | |
//ปริ้นช่องว่าง หลังจากสร้างพีระมิด | |
for (int j = i; j > 1; j--) { | |
System.out.print(" "); | |
} | |
//ขึ้นบรรทัดใหม่ หลังจากสร้างพีระมิดแต่ละชั้น | |
System.out.println(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoopStarExample4 { | |
public static void main(String[] args) { | |
for (int i = 5; i > 0; i--) { | |
for (int j = i; j < 5; j++) { | |
System.out.print(" "); | |
} | |
for (int j = i; j >= 1; j--) { | |
System.out.print("*"); | |
} | |
for (int j = i; j > 1; j--) { | |
System.out.print("*"); | |
} | |
for (int j = i; j < 5; j++) { | |
System.out.print(" "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoopStarExample5 { | |
public static void main(String[] args) { | |
//พีระมิด ฐานสามเหลี่ยม | |
for (int i = 5; i > 0; i--) { | |
for (int j = i; j > 1; j--) { | |
System.out.print(" "); | |
} | |
for (int j = i; j < 5; j++) { | |
System.out.print("*"); | |
} | |
for (int j = i; j <= 5; j++) { | |
System.out.print("*"); | |
} | |
for (int j = i; j > 1; j--) { | |
System.out.print(" "); | |
} | |
System.out.print("n"); | |
} | |
//พีระมิดฐานสามเหลี่ยมแบบกลับหัว | |
for (int i = 5; i > 0; i--) { | |
for (int j = i; j < = 5; j++) { | |
System.out.print(" "); | |
} | |
//เปลี่ยน ค่า j >= 2 จากเดิม j >= 1 | |
//เนื่องจากไม่ต้องการปริ้น ฐานสามเหลี่ยมทั้งสองรูป | |
for (int j = i; j >= 2; j--) { | |
System.out.print("*"); | |
} | |
for (int j = i; j > 2; j--) { | |
System.out.print("*"); | |
} | |
for (int j = i; j < = 5; j++) { | |
System.out.print(" "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment