Java實(shí)現(xiàn)簡單猜拳游戲
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡單猜拳游戲的具體代碼,供大家參考,具體內(nèi)容如下
看網(wǎng)上的猜拳游戲那么多,但都是用switch輸入數(shù)字,所以用if嵌套,寫一個(gè)簡單的猜拳游戲
package Game;
import java.util.Scanner;
import java.util.Random;//生成隨機(jī)數(shù),利用switch生成隨機(jī)的石頭,剪刀或者布
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("平局");
}
}
}
}
}
再為大家補(bǔ)充一段猜拳游戲代碼:
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中Collection集合常用API之?Collection存儲(chǔ)自定義類型對象的示例代碼
Collection是單列集合的祖宗接口,因此它的功能是全部單列集合都可以繼承使用的,這篇文章主要介紹了Java中Collection集合常用API?-?Collection存儲(chǔ)自定義類型對象,需要的朋友可以參考下2022-12-12
Java使用原型模式展現(xiàn)每日生活應(yīng)用案例詳解
這篇文章主要介紹了Java使用原型模式展現(xiàn)每日生活應(yīng)用案例,較為詳細(xì)的分析了原型模式的概念、原理及Java使用原型模式展現(xiàn)每日生活案例的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2018-05-05
SpringBoot整合H2數(shù)據(jù)庫的操作方法
H2是一個(gè)Java語言編寫的嵌入式數(shù)據(jù)庫,它不受平臺(tái)的限制,同時(shí)H2提供了一個(gè)十分方便的web控制臺(tái),用于操作和管理數(shù)據(jù)庫內(nèi)容,本文介紹SpringBoot整合H2數(shù)據(jù)庫的方法,感興趣的朋友一起看看吧2024-01-01
一文吃透Spring?Cloud?gateway自定義錯(cuò)誤處理Handler
這篇文章主要為大家介紹了一文吃透Spring?Cloud?gateway自定義錯(cuò)誤處理Handler方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
java 數(shù)據(jù)結(jié)構(gòu)單鏈表的實(shí)現(xiàn)
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)單鏈表的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-07-07
SpringMVC接收復(fù)雜集合對象(參數(shù))代碼示例
這篇文章主要介紹了SpringMVC接收復(fù)雜集合對象(參數(shù))代碼示例,舉接收List<String>、List<User>、List<Map<String,Object>>、User[]、User(bean里面包含List)幾種較為復(fù)雜的集合參數(shù),具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

