{"id":203929,"date":"2025-05-22T11:26:07","date_gmt":"2025-05-22T03:26:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203929\/"},"modified":"2025-05-22T11:26:07","modified_gmt":"2025-05-22T03:26:07","slug":"%e6%8e%a7%e5%88%b6%e6%b5%81%ef%bc%9a%e6%8e%8c%e6%8f%a1%e6%9d%a1%e4%bb%b6%e8%af%ad%e5%8f%a5%e5%92%8c%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203929\/","title":{"rendered":"\u63a7\u5236\u6d41\uff1a\u638c\u63e1\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u63a7\u5236\u6d41\uff1a\u638c\u63e1\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u63a7\u5236\u6d41\uff1a\u638c\u63e1\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733022631674bd3a780737.jpg\" class=\"aligncenter\"><\/p>\n<p>\u672c\u6307\u5357\u6db5\u76d6\u4e86 if-else\u3001for \u5faa\u73af\u3001while \u5faa\u73af\u7b49\u3002<\/p>\n<p>\u5728\u7f16\u7a0b\u4e2d\uff0c\u63a7\u5236\u6267\u884c\u6d41\u7a0b\u5bf9\u4e8e\u5728\u4ee3\u7801\u4e2d\u505a\u51fa\u51b3\u7b56\u548c\u91cd\u590d\u64cd\u4f5c\u81f3\u5173\u91cd\u8981\u3002 java \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u7ba1\u7406\u63a7\u5236\u6d41\uff0c\u5305\u62ec<strong>\u6761\u4ef6\u8bed\u53e5<\/strong>\u548c<strong>\u5faa\u73af<\/strong>\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u7814\u7a76\u8fd9\u4e9b\u57fa\u672c\u6982\u5ff5\uff0c\u63a2\u7d22\u5b83\u4eec\u7684\u5de5\u4f5c\u539f\u7406\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u6765\u521b\u5efa\u52a8\u6001\u548c\u54cd\u5e94\u5f0f\u7a0b\u5e8f\u3002<\/p>\n<h3> <strong>1.\u6761\u4ef6\u8bed\u53e5<\/strong> <\/h3>\n<h4> <strong>1.1 if-else \u8bed\u53e5<\/strong> <\/h4>\n<p>if-else \u8bed\u53e5\u5141\u8bb8\u60a8\u6839\u636e\u6761\u4ef6\u662f true \u8fd8\u662f false \u6765\u6267\u884c\u4ee3\u7801\u5757\u3002\u8fd9\u5c31\u50cf\u5728\u7a0b\u5e8f\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u68c0\u67e5\u70b9\uff0c\u5176\u4e2d\u67d0\u4e9b\u4ee3\u7801\u4ec5\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u624d\u8fd0\u884c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>if (condition) {\n    \/\/ code to execute if the condition is true\n} else {\n    \/\/ code to execute if the condition is false\n}\n<\/pre>\n<h4> <strong>\u793a\u4f8b\uff1a<\/strong> <\/h4>\n<pre>int marks = 75;\n\nif (marks &gt;= 60) {\n    system.out.println(\"passed with distinction!\");\n} else if (marks &gt;= 40) {\n    system.out.println(\"passed!\");\n} else {\n    system.out.println(\"failed.\");\n}\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u5206\u6570\u8fbe\u5230 60 \u5206\u6216\u66f4\u9ad8\uff0c\u201c\u4ee5\u4f18\u5f02\u6210\u7ee9\u901a\u8fc7\uff01\u201d\u5df2\u6253\u5370\u3002<\/li>\n<li>\u5982\u679c\u5206\u6570\u5728 40 \u5230 59 \u4e4b\u95f4\uff0c\u5219\u201c\u901a\u8fc7\uff01\u201d\u5df2\u6253\u5370\u3002<\/li>\n<li>\u5982\u679c\u5206\u6570\u4f4e\u4e8e 40 \u5206\uff0c\u5219\u201c\u5931\u8d25\u201d\u3002\u5df2\u6253\u5370\u3002<\/li>\n<\/ul>\n<h3> <strong>\u6311\u62181\uff1a<\/strong> <\/h3>\n<p>\u7f16\u5199\u4e00\u4e2a java \u7a0b\u5e8f\uff0c\u4f7f\u7528 if-else \u8bed\u53e5\u68c0\u67e5\u6570\u5b57\u662f\u6b63\u6570\u3001\u8d1f\u6570\u8fd8\u662f\u96f6\u3002\u4e3a\u6bcf\u79cd\u60c5\u51b5\u6253\u5370\u4e00\u6761\u9002\u5f53\u7684\u6d88\u606f\u3002<\/p>\n<h4> <strong>1.2 switch \u8bed\u53e5<\/strong> <\/h4>\n<p>switch \u8bed\u53e5\u662f\u53e6\u4e00\u79cd\u6839\u636e\u53d8\u91cf\u503c\u6267\u884c\u4ee3\u7801\u7684\u65b9\u6cd5\u3002\u5f53\u60a8\u9700\u8981\u5c06\u5355\u4e2a\u53d8\u91cf\u4e0e\u591a\u4e2a\u53ef\u80fd\u503c\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u5b83\u7279\u522b\u6709\u7528\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>switch (variable) {\n    case value1:\n        \/\/ code to execute if variable == value1\n        break;\n    case value2:\n        \/\/ code to execute if variable == value2\n        break;\n    \/\/ more cases...\n    default:\n        \/\/ code to execute if none of the cases match\n}\n<\/pre>\n<h4> <strong>\u793a\u4f8b\uff1a<\/strong> <\/h4>\n<pre>int dayofweek = 3;\nstring day;\n\nswitch (dayofweek) {\n    case 1:\n        day = \"sunday\";\n        break;\n    case 2:\n        day = \"monday\";\n        break;\n    case 3:\n        day = \"tuesday\";\n        break;\n    \/\/ more cases...\n    default:\n        day = \"invalid day\";\n        break;\n}\n\nsystem.out.println(\"today is: \" + day);\n<\/pre>\n<h3> <strong>2.\u5faa\u73af<\/strong> <\/h3>\n<p>\u5faa\u73af\u662f\u7f16\u7a0b\u4e2d\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u8ba9\u60a8\u591a\u6b21\u91cd\u590d\u4e00\u6bb5\u4ee3\u7801\u3002 java \u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u5faa\u73af\uff0c\u6bcf\u79cd\u7c7b\u578b\u9002\u5408\u4e0d\u540c\u7684\u573a\u666f\u3002<\/p>\n<h4> <strong>2.1 for \u5faa\u73af<\/strong> <\/h4>\n<p>\u5f53\u60a8\u4e8b\u5148\u77e5\u9053\u9700\u8981\u8fed\u4ee3\u591a\u5c11\u6b21\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528 for \u5faa\u73af\u3002\u5b83\u7531\u4e09\u90e8\u5206\u7ec4\u6210\uff1a\u521d\u59cb\u5316\u3001\u6761\u4ef6\u548c\u8fed\u4ee3\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>for (initialization; condition; iteration) {\n    \/\/ code to execute in each loop iteration\n}\n<\/pre>\n<h4> <strong>\u793a\u4f8b\uff1a<\/strong> <\/h4>\n<pre>for (int i = 1; i &lt;= 5; i++) {\n    system.out.println(\"iteration: \" + i);\n}\n<\/pre>\n<p>\u5728\u6b64\u5faa\u73af\u4e2d\uff1a<\/p>\n<ul>\n<li> \u6574\u6570 i = 1;\u521d\u59cb\u5316\u5faa\u73af\u8ba1\u6570\u5668 i.<\/li>\n<li> \u6211 &lt;= 5;\u8bbe\u7f6e\u5faa\u73af\u8fd0\u884c\u7684\u6761\u4ef6\uff08\u53ea\u8981 i \u7b49\u4e8e\u6216\u5c0f\u4e8e 5\uff09\u3002<\/li>\n<li> i++ \u5728\u6bcf\u6b21\u8fed\u4ee3\u540e\u5c06 i \u52a0 1\u3002<\/li>\n<\/ul>\n<h3> <strong>\u6311\u62182\uff1a<\/strong> <\/h3>\n<p>\u521b\u5efa\u4e00\u4e2a for \u5faa\u73af\uff0c\u6253\u5370\u524d 10 \u4e2a\u5076\u6570\u3002<\/p>\n<h4> <strong>2.2 while \u5faa\u73af<\/strong> <\/h4>\n<p>\u53ea\u8981\u6307\u5b9a\u6761\u4ef6\u4e3a\u771f\uff0cwhile \u5faa\u73af\u5c31\u4f1a\u7ee7\u7eed\u6267\u884c\u3002\u5f53\u4e8b\u5148\u4e0d\u77e5\u9053\u8fed\u4ee3\u6b21\u6570\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u5b83\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>while (condition) {\n    \/\/ code to execute while the condition is true\n}\n<\/pre>\n<h4> <strong>\u793a\u4f8b\uff1a<\/strong> <\/h4>\n<pre>int count = 0;\n\nwhile (count &lt; 3) {\n    system.out.println(\"count: \" + count);\n    count++;\n}\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u5faa\u73af\u6253\u5370 count \u7684\u503c\u5e76\u9012\u589e\u5b83\uff0c\u76f4\u5230 count \u4e0d\u518d\u5c0f\u4e8e 3\u3002<\/p>\n<h3> <strong>2.3 do-while \u5faa\u73af<\/strong> <\/h3>\n<p>do-while \u5faa\u73af\u4e0e while \u5faa\u73af\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4fdd\u8bc1\u5faa\u73af\u4f53\u81f3\u5c11\u6267\u884c\u4e00\u6b21\uff0c\u5373\u4f7f\u6761\u4ef6\u4ece\u4e00\u5f00\u59cb\u5c31\u662f false\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>do {\n    \/\/ code to execute at least once\n} while (condition);\n<\/pre>\n<h4> <strong>\u793a\u4f8b\uff1a<\/strong> <\/h4>\n<pre>int count = 0;\n\ndo {\n    system.out.println(\"count: \" + count);\n    count++;\n} while (count &lt; 3);\n<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5faa\u73af\u6253\u5370 count \u7684\u503c\u5e76\u9012\u589e\u5b83\uff0c\u5c31\u50cf while \u5faa\u73af\u4e00\u6837\uff0c\u4f46\u5b83\u786e\u4fdd\u4ee3\u7801\u81f3\u5c11\u8fd0\u884c\u4e00\u6b21\uff0c\u5373\u4f7f count \u4ece 3 \u6216\u66f4\u9ad8\u5f00\u59cb\u3002<\/p>\n<h3> <strong>2.4 break \u548ccontinue \u8bed\u53e5<\/strong> <\/h3>\n<ul>\n<li> <strong>break\uff1a<\/strong> \u7acb\u5373\u9000\u51fa\u5faa\u73af\uff0c\u8df3\u8fc7\u4efb\u4f55\u5269\u4f59\u7684\u8fed\u4ee3\u3002<\/li>\n<li> <strong>\u7ee7\u7eed\uff1a<\/strong> \u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u5e76\u8df3\u8f6c\u5230\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u3002<\/li>\n<\/ul>\n<h4> <strong>\u4f7f\u7528 break \u7684\u793a\u4f8b\uff1a<\/strong> <\/h4>\n<pre>for (int i = 1; i &lt;= 10; i++) {\n    if (i == 5) {\n        break; \/\/ exit the loop when i is 5\n    }\n    system.out.println(\"value of i: \" + i);\n}\n<\/pre>\n<h4> <strong>\u4f7f\u7528\u7ee7\u7eed\u7684\u793a\u4f8b\uff1a<\/strong> <\/h4>\n<pre>for (int i = 1; i &lt;= 5; i++) {\n    if (i == 3) {\n        continue; \/\/ Skip the iteration when i is 3\n    }\n    System.out.println(\"Value of i: \" + i);\n}\n<\/pre>\n<h3> <strong>\u6311\u62183\uff1a<\/strong> <\/h3>\n<p>\u7f16\u5199\u4e00\u4e2a\u5faa\u73af\uff0c\u6253\u5370\u4ece 1 \u5230 10 \u7684\u6570\u5b57\uff0c\u4f46\u8df3\u8fc7\u6570\u5b57 5\u3002<\/p>\n<h3> <strong>\u603b\u7ed3<\/strong> <\/h3>\n<p>\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\u63a7\u5236 java \u7a0b\u5e8f\u6d41\u7a0b\u7684\u8981\u70b9\u3002\u6211\u4eec\u63a2\u7d22\u4e86 if-else\u3001switch\u3001for\u3001while \u548c do-while \u5faa\u73af\uff0c\u4ee5\u53ca break \u548c continue \u8bed\u53e5\u3002<\/p>\n<p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u63a7\u5236\u6d41\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u66f4\u52a0\u52a8\u6001\u3001\u9ad8\u6548\u7684java\u7a0b\u5e8f\u3002\u5c1d\u8bd5\u6311\u6218\u6765\u5de9\u56fa\u60a8\u6240\u5b66\u7684\u77e5\u8bc6\uff01<\/p>\n<p>\u5728\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 java \u4e2d\u7684\u6570\u7ec4\u548c\u96c6\u5408\uff0c\u5b83\u4eec\u662f\u6709\u6548\u7ba1\u7406\u6570\u636e\u7ec4\u7684\u5173\u952e\u3002\u656c\u8bf7\u671f\u5f85\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u63a7\u5236\u6d41\uff1a\u638c\u63e1\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a7\u5236\u6d41\uff1a\u638c\u63e1\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af \u6536\u85cf&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203929","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203929"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203929\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}