{"id":40275,"date":"2024-11-26T11:21:41","date_gmt":"2024-11-26T03:21:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40275\/"},"modified":"2024-11-26T11:21:41","modified_gmt":"2024-11-26T03:21:41","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e5%9c%a8%e4%b8%80%e7%bb%84%e6%95%b0%e7%bb%84%e4%b8%ad%e7%8c%9c%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40275\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u5728\u4e00\u7ec4\u6570\u7ec4\u4e2d\u731c\u6570"},"content":{"rendered":"<blockquote><p>\n  \u672c\u6307\u5357\u63d0\u4f9b\u4e86\u5728 java \u6570\u7ec4\u4e2d\u731c\u6570\u7684\u660e\u786e\u6b65\u9aa4\uff1a1. \u5bfc\u5165 random \u7c7b\uff1b2. \u521b\u5efa\u5e76\u586b\u5145\u6570\u7ec4\uff1b3. \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u6570\u751f\u6210\u5668\uff1b4. \u751f\u6210\u4e00\u4e2a\u968f\u673a\u7d22\u5f15\uff1b5. \u731c\u6d4b\u6570\u5b57\uff1b6. \u68c0\u67e5\u731c\u6d4b\u662f\u5426\u6b63\u786e\u3002\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b\u4e5f\u5df2\u63d0\u4f9b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/18\/2024111815453773652.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u5728\u4e00\u7ec4\u6570\u7ec4\u4e2d\u731c\u6570\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u5728\u4e00\u7ec4\u6570\u7ec4\u4e2d\u731c\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u7684\u4e00\u7ec4\u6570\u7ec4\u4e2d\u731c\u6570<\/strong><\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>1. \u5bfc\u5165 java.util.Random<\/strong><\/p>\n<p>\u8981\u751f\u6210\u968f\u673a\u6570\uff0c\u4f60\u9700\u8981\u5bfc\u5165 java.util.Random \u7c7b\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.Random;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\u5e76\u586b\u5145\u6570\u636e<\/strong><\/p>\n<p>\u521b\u5efa\u8981\u731c\u6570\u7684\u6570\u7ec4\u5e76\u7528\u6570\u5b57\u586b\u5145\u5b83\u3002<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u6570\u751f\u6210\u5668<\/strong><\/p>\n<p>\u4f7f\u7528 Random \u7c7b\u521b\u5efa\u4e00\u4e2a\u968f\u673a\u6570\u751f\u6210\u5668\u3002<\/p>\n<pre>Random random = new Random();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u751f\u6210\u4e00\u4e2a\u968f\u673a\u7d22\u5f15<\/strong><\/p>\n<p>\u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668\u751f\u6210\u4e00\u4e2a\u4e0e\u6570\u7ec4\u957f\u5ea6\u76f8\u540c\u7684\u968f\u673a\u7d22\u5f15\u3002<\/p>\n<pre>int index = random.nextInt(numbers.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u731c\u6d4b\u6570\u5b57<\/strong><\/p>\n<p>\u4f7f\u7528\u751f\u6210\u7684\u7d22\u5f15\u4ece\u6570\u7ec4\u4e2d\u731c\u6d4b\u6570\u5b57\u3002<\/p>\n<pre>int guess = numbers[index];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u68c0\u67e5\u731c\u6d4b\u662f\u5426\u6b63\u786e<\/strong><\/p>\n<p>\u63d0\u793a\u7528\u6237\u8f93\u5165\u731c\u6d4b\u5e76\u5c06\u5176\u4e0e\u731c\u6d4b\u7684\u6570\u5b57\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<pre>int userGuess = Integer.parseInt(scanner.nextLine());\nif (userGuess == guess) {\n    System.out.println(\"\u4f60\u731c\u5bf9\u4e86\uff01\");\n} else {\n    System.out.println(\"\u4f60\u731c\u9519\u4e86\uff01\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5b8c\u6574\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import java.util.Random;\nimport java.util.Scanner;\n\npublic class GuessNumber {\n\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n\n        \/\/ \u521b\u5efa\u6570\u7ec4\u5e76\u586b\u5145\u6570\u636e\n        int[] numbers = {1, 2, 3, 4, 5};\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u6570\u751f\u6210\u5668\n        Random random = new Random();\n\n        \/\/ \u751f\u6210\u4e00\u4e2a\u968f\u673a\u7d22\u5f15\n        int index = random.nextInt(numbers.length);\n\n        \/\/ \u731c\u6d4b\u6570\u5b57\n        int guess = numbers[index];\n\n        \/\/ \u63d0\u793a\u7528\u6237\u8f93\u5165\u731c\u6d4b\n        System.out.println(\"\u731c\u4e00\u4e2a\u6570\u5b57\uff1a\");\n        int userGuess = Integer.parseInt(scanner.nextLine());\n\n        \/\/ \u68c0\u67e5\u731c\u6d4b\u662f\u5426\u6b63\u786e\n        if (userGuess == guess) {\n            System.out.println(\"\u4f60\u731c\u5bf9\u4e86\uff01\");\n        } else {\n            System.out.println(\"\u4f60\u731c\u9519\u4e86\uff01\");\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u5728\u4e00\u7ec4\u6570\u7ec4\u4e2d\u731c\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6307\u5357\u63d0\u4f9b\u4e86\u5728 java \u6570\u7ec4\u4e2d\u731c\u6570\u7684\u660e\u786e\u6b65\u9aa4\uff1a1. \u5bfc\u5165 random \u7c7b\uff1b2. \u521b\u5efa\u5e76\u586b\u5145\u6570\u7ec4\uff1b3. \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u6570\u751f\u6210\u5668\uff1b4. \u751f\u6210\u4e00\u4e2a\u968f\u673a\u7d22\u5f15\uff1b5. \u731c\u6d4b\u6570\u5b57\uff1b6. \u68c0\u67e5\u731c\u6d4b\u662f\u5426\u6b63\u786e\u3002\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b\u4e5f\u5df2\u63d0\u4f9b\u3002 \u5982\u4f55\u5728 Java \u4e2d\u7684\u4e00\u7ec4\u6570\u7ec4\u4e2d\u731c\u6570 \u65b9\u6cd5\uff1a 1. \u5bfc\u5165 java.util.Random \u8981\u751f\u6210\u968f\u673a\u6570\uff0c\u4f60\u9700\u8981\u5bfc\u5165 java.util.Random \u7c7b\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import java.util.Random; \u767b\u5f55\u540e\u590d\u5236 2. \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\u5e76\u586b\u5145\u6570\u636e \u521b\u5efa\u8981\u731c\u6570\u7684\u6570\u7ec4\u5e76\u7528\u6570\u5b57\u586b\u5145\u5b83\u3002 int[] numbers = {1, 2, 3, 4, 5}; \u767b\u5f55\u540e\u590d\u5236 3. \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u6570\u751f\u6210\u5668 \u4f7f\u7528 Random \u7c7b\u521b\u5efa\u4e00\u4e2a\u968f\u673a\u6570\u751f\u6210\u5668\u3002 Random random = new Random(); \u767b\u5f55\u540e\u590d\u5236 4. \u751f\u6210\u4e00\u4e2a\u968f\u673a\u7d22\u5f15 \u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668\u751f\u6210\u4e00\u4e2a\u4e0e\u6570\u7ec4\u957f\u5ea6\u76f8\u540c\u7684\u968f\u673a\u7d22\u5f15\u3002 int index = random.nextInt(numbers.length); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-40275","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=40275"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40275\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}