ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2023ǯ06·î05Æü(·î) 11:30:51ÍúÎò
¡¡Î¾Êý¤Îµ½Ò¤Ë¤Ä¤¤¤Æ·ÇºÜ¤·¤Þ¤¹¤¬¡¢¤è¤êŬ¤·¤Æ¤¤¤ëµË¡¤òÀè¤ËÄ󼨤·¤Þ¤¹¡£
¡¡¥²¡¼¥àÆâ¤Î¥¥ã¥é¥¯¥¿¡¼¤¬¡¢Áö¤Ã¤Æ¤¤¤ë¾õÂÖ¡¢¥¸¥ã¥ó¥×¤·¤Æ¤¤¤ë¾õÂÖ¡¢¹¶·â¤·¤Æ¤¤¤ë¾õÂ֤Ȥ¤¤Ã¤¿Ê£¿ô¤Î¾õÂÖ¤ò»ý¤Ã¤Æ¤¤¤ë¤È¤·¤Þ¤¹¡£
¡¡¤³¤ì¤é¤Î¾õÂ֤˱þ¤¸¤ÆŬÀڤʥ¢¥Ë¥á¡¼¥·¥ç¥ó¤òɽ¼¨¤·¤¿¤¤¤È¤¡¢enum¤Èswitchʸ¤ò»È¤¦¤È¥³¡¼¥É¤¬¤¤ì¤¤¤Ë¤Þ¤È¤Þ¤ê¤Þ¤¹¡£
// enum¤Ç¥¥ã¥é¥¯¥¿¡¼¤Î¾õÂÖ¤òÄêµÁ public enum CharacterState { Running, Jumping, Attacking } // ¥¥ã¥é¥¯¥¿¡¼¤Î¾õÂÖ CharacterState state = CharacterState.Running; // switchʸ¤ò»È¤Ã¤Æ¡¢¾õÂ֤˱þ¤¸¤¿¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ switch (state) { case CharacterState.Running: // Running¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ break; case CharacterState.Jumping: // Jumping¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ break; case CharacterState.Attacking: // Attacking¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ break; default: // ̤ÄêµÁ¤Î¾õÂ֤ξì¹ç¤Î½èÍý break; }
¡¡°Ê²¼¤Î¤è¤¦¤Ëifʸ¤ò»ÈÍѤ·¤Æ¤âƱ¤¸¤è¤¦¤Ë½èÍý¤Ç¤¤Þ¤¹¤¬¡¢switchʸ¤ò»È¤¦Êý¤¬²ÄÆÉÀ¤¬¹â¤¤¤Ç¤¹¡£
Æäˡ¢¾õÂÖ¤¬¹¹¤ËÁý¤¨¤ë¾ì¹ç¡¢ifʸ¤Ç¤ÏÊ£»¨¤µ¤¬Áý¤·¤Æ¤·¤Þ¤¤¤Þ¤¹¡£
CharacterState state = CharacterState.Running; // ifʸ¤Ç½ñ¤Ä¾¤¹ if (state == CharacterState.Running) { // Running¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ } else if (state == CharacterState.Jumping) { // Jumping¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ } else if (state == CharacterState.Attacking) { // Attacking¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ } else { // ̤ÄêµÁ¤Î¾õÂ֤ξì¹ç¤Î½èÍý }
¡¡Å¨¥¥ã¥é¥¯¥¿¡¼¤¬¥×¥ì¥¤¥ä¡¼¤Îµ÷Î¥¤Ë±þ¤¸¤Æ¹ÔÆ°¤òÊѤ¨¤ë¾ì¹ç¡¢ifʸ¤¬ÊØÍø¤Ç¤¹¡£
µ÷Î¥¤¬10m°Ê²¼¤Ê¤é¹¶·â¡¢20m°Ê²¼¤Ê¤éÀܶᡢ¤½¤ì°Ê¾å¤Ê¤éÂÔµ¡¤È¤¤¤Ã¤¿¶ñÂÎŪ¤ÊÈϰϤ˴ð¤Å¤¤¤¿¾ò·ï¤òÀßÄê¤Ç¤¤Þ¤¹¡£
float distance = // ¥×¥ì¥¤¥ä¡¼¤È¤Îµ÷Î¥ // ifʸ¤ò»È¤Ã¤Æ¡¢µ÷Î¥¤Ë±þ¤¸¤¿¹ÔÆ°¤òÁªÂò if (distance <= 10) { // ¹¶·â¤¹¤ë } else if (distance <= 20) { // À᤹ܶ¤ë } else { // ÂÔµ¡¤¹¤ë }
¡¡°Ê²¼¤Î¤è¤¦¤Ëswitchʸ¤ò»ÈÍѤ·¤Æ¤â½èÍý¤Ï²Äǽ¤Ç¤¹¤¬¡¢²ÄÆÉÀ¤â°¤¯¡¢ÈϰϤ˴ð¤Å¤¯¾ò·ïȽÃǤ¬Æñ¤·¤¯¤Ê¤ë¤¿¤á¡¢¤³¤Î¾ì¹ç¤Ïifʸ¤ÎÊý¤¬Å¬¤·¤Æ¤¤¤Þ¤¹¡£
float distance = // ¥×¥ì¥¤¥ä¡¼¤È¤Îµ÷Î¥ // switchʸ¤Ç½ñ¤Ä¾¤¹¡ÊÈó¿ä¾©¡Ë switch (distance) { case var d when d <= 10: // ¹¶·â¤¹¤ë break; case var d when d <= 20: // À᤹ܶ¤ë break; default: // ÂÔµ¡¤¹¤ë break; }
¡¡¥²¡¼¥àÆâ¤ÎÆÃÄê¤Î¥¤¥Ù¥ó¥È¡Ê¤¿¤È¤¨¤Ð¡¢¥×¥ì¥¤¥ä¡¼¤¬ÊõÈ¢¤ò³«¤±¤ë¡¢Å¨¤òÅݤ¹¤Ê¤É¡Ë¤¬È¯À¸¤·¤¿¤È¤¤Ë¡¢¤½¤ì¤¾¤ì°Û¤Ê¤ë½èÍý¤ò¹Ô¤¤¤¿¤¤¾ì¹ç¡¢switchʸ¤¬Å¬¤·¤Æ¤¤¤Þ¤¹¡£
¡¡ÀßÄꤵ¤ì¤Æ¤¤¤ëÈϰϤ¬»öÁ°¤Ë·è¤Þ¤Ã¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï¡¢¤ä¤Ï¤ê¡¢enum ¤È¤ÎÁȤ߹ç¤ï¤»¤¬¸ú²ÌŪ¤Ç¤¹¡£
// enum¤Ç¥¤¥Ù¥ó¥È¤Î¼ïÎà¤òÄêµÁ public enum GameEvent { OpenChest, DefeatEnemy, ReachGoal } // ¥²¡¼¥à¥¤¥Ù¥ó¥È GameEvent gameEvent = GameEvent.OpenChest; // switchʸ¤ò»È¤Ã¤Æ¡¢¥¤¥Ù¥ó¥È¤Ë±þ¤¸¤¿½èÍý¤ò¼Â¹Ô switch (gameEvent) { case GameEvent.OpenChest: // ÊõÈ¢¤ò³«¤±¤ë½èÍý¡Ê¥¢¥¤¥Æ¥à¤ò³ÍÆÀ¤¹¤ëÅù¡Ë break; case GameEvent.DefeatEnemy: // Ũ¤òÅݤ¹½èÍý¡Ê·Ð¸³ÃͤòÆÀ¤ëÅù¡Ë break; case GameEvent.ReachGoal: // ¥´¡¼¥ë¤ËÅþ㤷¤¿½èÍý¡Ê¥ì¥Ù¥ë¥¯¥ê¥¢Åù¡Ë break; default: // ̤ÄêµÁ¤Î¥¤¥Ù¥ó¥È¤Î¾ì¹ç¤Î½èÍý break; }
¡¡°Ê²¼¤Î¤è¤¦¤Ëifʸ¤ò»ÈÍѤ·¤Æ¤â½èÍý¤Ç¤¤Þ¤¹¤¬¡¢switchʸ¤ÎÊý¤¬åºÎï¤Ë¤Þ¤È¤Þ¤ê¡¢¤Þ¤¿ÃͤÎÈϰϤ¬¸ÂÄꤵ¤ì¤ë¤¿¤á¡¢Æɤߤ䤹¤µ¤È¥Ð¥°¤ÎȯÀ¸¤òËɤ°ÅÀ¤ÇÍ¥¤ì¤Æ¤¤¤Þ¤¹¡£
GameEvent gameEvent = GameEvent.OpenChest; // ifʸ¤Ç½ñ¤Ä¾¤¹ if (gameEvent == GameEvent.OpenChest) { // ÊõÈ¢¤ò³«¤±¤ë½èÍý¡Ê¥¢¥¤¥Æ¥à¤ò³ÍÆÀ¤¹¤ëÅù¡Ë } else if (gameEvent == GameEvent.DefeatEnemy) { // Ũ¤òÅݤ¹½èÍý¡Ê·Ð¸³ÃͤòÆÀ¤ëÅù¡Ë } else if (gameEvent == GameEvent.ReachGoal) { // ¥´¡¼¥ë¤ËÅþ㤷¤¿½èÍý¡Ê¥ì¥Ù¥ë¥¯¥ê¥¢Åù¡Ë } else { // ̤ÄêµÁ¤Î¥¤¥Ù¥ó¥È¤Î¾ì¹ç¤Î½èÍý }
¡¡¤³¤ì¤é¤Î¥±¡¼¥¹¤è¤¦¤Ë¡¢switchʸ¤Èifʸ¤Ï¤½¤ì¤¾¤ì°Û¤Ê¤ë¾õ¶·¤Ë¤ª¤¤¤ÆÍ¥¤ì¤¿Æɤߤ䤹¤µ¤È¥Ñ¥Õ¥©¡¼¥Þ¥ó¥¹¤òÄ󶡤¹¤ë¤³¤È¤¬ÌÜŪ¤Ç¤¹¡£
¡¡Àè¤Û¤É¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤òǰƬ¤ËÃÖ¤¤¤Æ¡¢¤½¤ì¤¾¤ì¤ÎÆÃħ¤ò¸«Ä¾¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
¡¡switchʸ¤Ï¡¢¤¢¤ëÊÑ¿ô¤¬ÆÃÄê¤ÎÃͤò»ý¤Ã¤Æ¤¤¤ë¤«¤É¤¦¤«¤ò·«¤êÊÖ¤·³Îǧ¤¹¤ëɬÍפ¬¤¢¤ë¾ì¹ç¤ËÊØÍø¤Ç¤¹¡£
Æäˡ¢¤³¤ÎÊÑ¿ô¤ÎÃͤ¬¤¢¤é¤«¤¸¤á¸ÂÄꤵ¤ì¤¿ÈÏ°ÏÆâ(¥»¥Ã¥È)¤Ë¤¢¤ë¤³¤È¤¬Ê¬¤«¤Ã¤Æ¤¤¤ë¾ì¹ç¡ÊÎ㡧¥¥ã¥é¥¯¥¿¡¼¤Î¾õÂ֤䥲¡¼¥à¥¤¥Ù¥ó¥È¤Ê¤É¡Ë¤Ë¤Ï¡¢enum¤È°ì½ï¤Ë»È¤¦¤ÈÈó¾ï¤Ë¸ú²ÌŪ¤Ç¤¹¡£
¡¡¤³¤Î¥»¥Ã¥È¤¬¤¢¤é¤«¤¸¤áÃΤé¤ì¤Æ¤¤¤Æ¡¢¤½¤ì¤é¤ÎÁªÂò»è¤¬Áê¸ß¤ËÇÓ¾Ū¤Ç¤¢¤ë¾ì¹ç¡¢switchʸ¤Ï¥³¡¼¥É¤ò¤¤ì¤¤¤ËÊݤĤΤËÌòΩ¤Á¤Þ¤¹¡£¤½¤ì¤Ï¤Þ¤¿¡¢¤¢¤ëÊÑ¿ô¤¬¼è¤ê¤¦¤ëÃͤò¸«¤Ä¤±¤ä¤¹¤¯¤¹¤ë¤¿¤á¡¢¥Ð¥°¤ò¸«¤Ä¤±¤ë¤Î¤ËÌòΩ¤Ä²ÄǽÀ¤¬¤¢¤ê¤Þ¤¹¡£
¡¡°ìÊý¡¢ifʸ¤Ï¡¢°ìÏ¢¤Î¾ò·ï¤ò½çÈ֤˳Îǧ¤·¡¢ºÇ½é¤Ë¿¿¤È¤Ê¤ë¾ò·ï¤ËÂбþ¤¹¤ë½èÍý¤ò¼Â¹Ô¤·¤¿¤¤¾ì¹ç¤ËÊØÍø¤Ç¤¹¡£
ÊÑ¿ô¤¬ÆÃÄê¤ÎÈÏ°ÏÆâ¤Ë¤¢¤ë¤«¤É¤¦¤«¤ò³Îǧ¤¹¤ë¾ì¹ç¡ÊÎ㡧Ũ¥¥ã¥é¥¯¥¿¡¼¤ÎAI¹ÔÆ°¤Ê¤É¡Ë¤Ë¤Ï¡¢ifʸ¤¬¼«Á³¤Ë¥Õ¥£¥Ã¥È¤·¤Þ¤¹¡£
¡¡Æäˡ¢Ï¢Â³Åª¤ÊÈϰϤޤ¿¤Ï¾ò·ï¤¬½ÅÊ£¤¹¤ë²ÄǽÀ¤¬¤¢¤ë¾ì¹ç¤ËÍÍѤǤ¹¡£
°Û¤Ê¤ëÈϰϤ˸٤ë¾ò·ï¤ò°·¤¦¤³¤È¤¬²Äǽ¤Ç¤¢¤ê¡¢¤½¤ì¤¬switchʸ¤Î¾ì¹ç¤Ë¤Ïµ½Ò¤¬º¤Æñ¤Ë¤Ê¤ê¤Þ¤¹¡£
¡¡switchʸ¤Èifʸ¤Î»È¤¤Ê¬¤±Êý¤Î¼ÂÁõÎã¤òÄ󼨤·¤Þ¤·¤¿¡£
¤½¤ì¤¾¤ì¤Î¥Ý¥¤¥ó¥È¤òÍý²ò¤¤¤¿¤À¤±¤ì¤Ð¹¬¤¤¤Ç¤¹¡£
¡¡ºÇ½ªÅª¤Ë¤Ï¡¢»È¤¤¤ä¤¹¤µ¡¢¥³¡¼¥É¤Î²ÄÆÉÀ¡¢¤½¤·¤Æ¤¢¤Ê¤¿¤¬¤É¤ÎÄøÅÙ¤ÎÀºÅ٤Ǿò·ï¤òÀ©¸æ¤·¤¿¤¤¤«¤Ë¤è¤ê¤Þ¤¹¡£
ξ¼Ô¤Î°ã¤¤¤òÍý²ò¤·¡¢¼«¿È¤Î¥Ë¡¼¥º¤ËºÇŬ¤ÊÁªÂò¤ò¹Ô¤¦¤³¤È¤¬½ÅÍפǤ¹¡£
¡¡¤É¤Á¤é¤ÎÊý¤¬Å¬¤·¤Æ¤¤¤ë¤«¤Ï¡¢¼ÂºÝ¤Ë¼«Ê¬¤ÇξÊý¤Î¥±¡¼¥¹¤ò½ñ¤¤¤Æ¤ß¤Ê¤¬¤é»È¤¤Êý¤ò½¬ÆÀ¤·¤Æ¤¤¤¯¤³¤È¤ò¤ª´«¤á¤·¤Þ¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯