{"id":208205,"date":"2025-07-08T14:55:08","date_gmt":"2025-07-08T06:55:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208205\/"},"modified":"2025-07-08T14:55:08","modified_gmt":"2025-07-08T06:55:08","slug":"%e4%bb%a3%e7%a0%81%e7%bc%96%e8%af%91%e4%bd%86-for-%e5%be%aa%e7%8e%af%e6%b2%a1%e6%9c%89%e8%a2%ab%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208205\/","title":{"rendered":"\u4ee3\u7801\u7f16\u8bd1\u4f46 for \u5faa\u73af\u6ca1\u6709\u88ab\u6267\u884c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4ee3\u7801\u7f16\u8bd1\u4f46 for \u5faa\u73af\u6ca1\u6709\u88ab\u6267\u884c<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 10:48:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u4ee3\u7801\u7f16\u8bd1\u4f46 for \u5faa\u73af\u6ca1\u6709\u88ab\u6267\u884c\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<pre>package main\n\nimport \"fmt\"\n\n\/\/ I want to make a code that outputs a series of \"n\" odd numbers.\n\/\/ For example-&gt; Input : 7 --&gt; Output : 1 , 3 , 5 , 7\n\nfunc main(){\n\nvar n int\nvar i int\n\nfmt.Println(\"I'll give you a series of odd numbers to n \") \/\/ I'm asking which number do I want the series to stop.\n\nfmt.Scanln(&amp;n)\n\n  if n%2 == 1 {\n\n    for i=0 ; i &gt;= n; i++ { \/\/ Here it's supposed to print out the series but at least something\n                            \/\/ The whole code compiles but when I execute it, the program just takes the number that I ask in the Scanln\n      var odd int          \/\/ and it stops there. What am I doing wrong? The for cycle? \n\n        odd = (i*2) - 1\n\n      fmt.Printf(disp)\n\n    }\n\n  } else {\n\n    fmt.Println(\"It's round\") \/\/I've put the for in an (if\/else) just to see if it somehow it didn't read until the for part.\n                              \/\/But the (if\/else) works but the for cycle doesn't. I'm so confused.\n  }\n\n}\n<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u8ba4\u4e3a\u60a8\u7684\u95ee\u9898\u5728\u4e8e\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre>...\nfor i=0 ; i &gt;= n; i++ {\n...\n<\/pre>\n<p>for \u5faa\u73af\u7684\u6761\u4ef6\u5bf9\u4e8e\u5185\u90e8\u5faa\u73af\u6765\u8bf4\u5e94\u8be5\u4e3a\u771f\u3002<\/p>\n<p>\u4f46\u662f\u8fd9\u91cc\u7684 <code>i<\/code> \u4ece 0 \u5f00\u59cb\uff0c\u5e76\u4e14 <code>n<\/code> \u603b\u662f\u66f4\u9ad8\uff0c\u9664\u975e\u60a8\u8bbe\u7f6e\u4e3a 0\u3002<\/p>\n<p>\u5c06 <code>i<\/code> \u7684\u6761\u4ef6\u66f4\u6539\u4e3a\u5c0f\u4e8e <code>n<\/code> \u5373\u53ef\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4ee3\u7801\u7f16\u8bd1\u4f46 for \u5faa\u73af\u6ca1\u6709\u88ab\u6267\u884c\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-208205","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208205","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=208205"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208205\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}