Java實現(xiàn)簡單猜拳游戲
本文實例為大家分享了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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot統(tǒng)一處理全局異常的實戰(zhàn)教程
最近在做項目時需要對異常進行全局統(tǒng)一處理,所以下面這篇文章主要給大家介紹了關(guān)于Spring?Boot統(tǒng)一處理全局異常的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2021-12-12
Spring數(shù)據(jù)庫事務(wù)的實現(xiàn)機制講解
這篇文章主要介紹了Spring數(shù)據(jù)庫事務(wù)的實現(xiàn)機制講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
淺談Java數(shù)據(jù)結(jié)構(gòu)之稀疏數(shù)組知識總結(jié)
今天帶大家了解一下Java稀疏數(shù)組的相關(guān)知識,文中有非常詳細的介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
freemarker?jsp?java內(nèi)存方式實現(xiàn)分頁示例
這篇文章主要介紹了freemarker?jsp?java內(nèi)存方式實現(xiàn)分頁示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
SpringBoot多數(shù)據(jù)源配置方式以及報錯問題的解決
這篇文章主要介紹了SpringBoot多數(shù)據(jù)源配置方式以及報錯問題的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

