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

您的位置:首頁技術(shù)文章
文章詳情頁

Java實現(xiàn)簡單猜拳游戲

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

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

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

package Game;import java.util.Scanner;import java.util.Random;//生成隨機數(shù),利用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表示人出的數(shù) * computer表示電腦出的數(shù) */ 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='剪刀'; //數(shù)字字符化 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); } }

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

標簽: Java
相關文章:
主站蜘蛛池模板: 尚志市| 溧阳市| 海盐县| 夹江县| 五峰| 民权县| 饶平县| 玉溪市| 珲春市| 日照市| 来安县| 宿州市| 久治县| 山阳县| 黔江区| 江陵县| 尤溪县| 张家口市| 大理市| 长沙市| 多伦县| 儋州市| 平泉县| 永川市| 邢台市| 禹城市| 台中市| 浙江省| 福贡县| 井陉县| 将乐县| 南京市| 甘泉县| 沧州市| 卓资县| 兴隆县| 唐山市| 湄潭县| 自贡市| 安乡县| 莱西市|