enumColors{
RED,
BLUE,
YELLOW,
GREEN
}publicclassMain{publicstaticvoidmain(String[] args){Colors red =Colors.RED;System.out.println(red.ordinal());// 0}}
red.ordinal()จากโค้ดด้านบนส่งคืน 0
วิธีใช้ Enum ในคำสั่ง Switch
ตอนนี้เราจะเรียนรู้วิธีใช้enumใน คำสั่ง switch :
publicclassMain{enumColors{
RED,
BLUE,
YELLOW,
GREEN
}publicstaticvoidmain(String[] args){Colors myColor =Colors.YELLOW;switch(myColor){case RED:System.out.println("The color is red");break;case BLUE:System.out.println("The color is blue");break;case YELLOW:System.out.println("The color is yellow");break;case GREEN:System.out.println("The color is green");break;}}}
enumColors{
RED,
BLUE,
YELLOW,
GREEN
}publicclassMain{publicstaticvoidmain(String[] args){for(Colors allColors :Colors.values()){System.out.println(allColors);/*
RED
BLUE
YELLOW
GREEN
*/}}}
การวนซ้ำ whileเป็นหนึ่งในประเภทการวนซ้ำพื้นฐานใน Java และสามารถใช้เพื่อวัตถุประสงค์หลายประการในโปรแกรม เช่นเดียวกับfor loop คุณสามารถใช้ while loop เพื่อดำเนินการตามเงื่อนไขได้ ในขณะที่ วนซ้ำ ตัวนับจะไม่เปิดเสมอไป จำนวนครั้งของ การวน ซ้ำ while ขึ้นอยู่กับความถี่ที่เงื่อนไขการวนซ้ำคืนค่า true การกำหนดค่าเริ่มต้นใน ลูป while อาจเป็นทาง เลือกในกรณีส่วนใหญ่ ไม่เหมือนใน ลูป for บางครั้ง การวน ซ้ำ whileไม่ได้ถูกดำเนินการแบบวนซ้ำเสมอไป ลองยกตัวอย่างที่เป็นรูปธรรม:
System.out.println("You Input a wrong number");int i = input.nextInt();while(i==1||i==2||i==3||i==4){System.out.println("inside the while loop block");}//if the user chooses any number different from 1,2,3 and 4.System.out.println("outside the while loop block")
System.out.println("Select an option\n"+"1.Add Users\n"+"2.Delete Users");int x = input.nextInt();while(x==1||x==2){//Do something}System.out.println("You Input a wrong number");
ในโปรแกรมข้างต้น จะใช้ while loop เพื่อตรวจสอบอินพุตของผู้ใช้และให้แน่ใจว่าผู้ใช้เลือก 1 หรือ 2 หากผู้ใช้เลือกหมายเลขอื่นSystem.out.println("You Input a wrong number"); .
คำสั่งแบบมีเงื่อนไขพร้อม while loop
int i =0;while(i<10){
i++;if(i==5){continue;}System.out.println(i);}
importjava.util.*;publicclassMain{staticScanner input =newScanner(System.in);staticboolean hasWon;staticint count;staticint guess;publicstaticvoid main (String[] args){System.out.println("Welcome to guess world");System.out.println("Do you want to start the game? ");System.out.println("1.Yes\n"+"2.No");int option= input.nextInt();int guessNumber =(int)Math.floor(Math.random()*10+1);if(!( option ==2)){System.out.println("Welcome, choose a guess between 1 and 10\n"+"you have only ten trials");while(count <=10){int userGuess = input.nextInt();if(userGuess == guessNumber){
hasWon =true;System.out.println("Congratulation, you guessed right at "+ count +" count");
count++;break;}elseif(userGuess < guessNumber){
hasWon =false;System.out.println("Oops, you guessed a lower number, try an higher one");
count++;}elseif(userGuess > guessNumber){System.out.println("Oops, you guessed an high number, try an lower one");
hasWon =false;}}if(hasWon ==false){System.out.println("You trials exceeds 10");}}}}
GO TO FULL VERSION