久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Java實現簡單猜拳游戲

瀏覽:2日期:2022-08-19 09:57:50

本文實例為大家分享了Java實現簡單猜拳游戲的具體代碼,供大家參考,具體內容如下

看網上的猜拳游戲那么多,但都是用switch輸入數字,所以用if嵌套,寫一個簡單的猜拳游戲

package Game;import java.util.Scanner;import java.util.Random;//生成隨機數,利用switch生成隨機的石頭,剪刀或者布public class CaiQuan { public static void main(String[] args) { while(true) { System.out.println('請輸入石頭,剪刀或者布'); Scanner sc = new Scanner(System.in); String quantou = sc.next(); int month = (int)(3*Math.random())+1; String com;//為電腦的出拳生成字符串 //電腦出拳 switch(month) { case 1: com = '石頭'; break; case 2: com = '剪刀'; break; case 3: com = '布'; break; } if(quantou.equals('石頭')) { if(month==1) { System.out.println('你出的是石頭,電腦出的是石頭'); System.out.println('平局'); } else if(month==2) { System.out.println('你出的是石頭,電腦出的是剪刀'); System.out.println('你贏了'); } else if(month==3){ System.out.println('你出的是石頭,電腦出的是布'); System.out.println('你輸了'); } } else if(quantou.equals('剪刀')) { if(month==1) { System.out.println('你出的是剪刀,電腦出的是石頭'); System.out.println('你輸了'); } else if(month==2) { System.out.println('你出的是剪刀,電腦出的是剪刀'); System.out.println('平局'); } else if(month==3){ System.out.println('你出的是剪刀,電腦出的是布'); System.out.println('你贏了'); } } else if(quantou.equals('布')) { if(month==1) { System.out.println('你出的是布,電腦出的是石頭'); System.out.println('你贏了'); } else if(month==2) { System.out.println('你出的是布,電腦出的是剪刀'); System.out.println('你輸了'); } else if(month==3) { System.out.println('你出的是布,電腦出的是布'); System.out.println('平局'); } } } }}

再為大家補充一段猜拳游戲代碼:

import java.util.Scanner;import java.util.Random;public class GuessingBoxing { public static void main(String[] args) { while(true) { System.out.println('----猜拳游戲----'); System.out.println('請出拳(1、剪刀 2、石頭 3.布)'); Scanner in=new Scanner(System.in); /** * people表示人出的數 * computer表示電腦出的數 */ int people=in.nextInt(); int computer=(int)(Math.random()*3+1); f(people,computer); System.out.println(); System.out.println(); //輸入完成,開始判斷輸贏 } } private static void f(int people, int computer) { String logo1='剪刀'; //數字字符化 String logo2='剪刀'; switch(people) { case 1: logo1='剪刀'; break; case 2: logo1='石頭'; break; case 3: logo1='布'; } switch(computer) { case 1: logo2='剪刀'; break; case 2: logo2='石頭'; break; case 3: logo2='布'; } if(people==computer) { System.out.println('平局 你出的是:'+logo1+' 電腦出的是'+logo1); }else if(people==1&&computer==2||people==2&&computer==3||people==3&&computer==1) { System.out.println('你輸了 你出的是:'+logo1+' 電腦出的是'+logo2); }else System.out.println('你贏了 你出的是:'+logo1+' 電腦出的是:'+logo2); } }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 东兴市| 旬邑县| 安国市| 达尔| 古田县| 沂水县| 秭归县| 资溪县| 泰兴市| 西城区| 元谋县| 东阳市| 祁连县| 正阳县| 花莲市| 上栗县| 新宁县| 沅陵县| 定结县| 永新县| 浦北县| 洱源县| 柯坪县| 海兴县| 武宁县| 常山县| 林州市| 黄石市| 郎溪县| 舒城县| 新民市| 通化市| 普陀区| 玉屏| 西安市| 邛崃市| 平顶山市| 密山市| 三河市| 南召县| 贡嘎县|