package homeWork;
import java.util.Scanner;
public class PlayGame2 {
public static void main(String[] args) {
// 猜拳游戲
Scanner input = new Scanner(System.in);
//1. 提示游戲 玩法
System.out.println("歡迎參加 猜拳游戲!!! \n 請(qǐng)選擇(1--3) \n 1.石頭 \n 2.剪刀 \n 3.布\n");
// 新建變量 comBoxing 電腦出拳 playInput 用戶出拳
int playInput = 0;
int personScore = 0;
int computerScore = 0;
int count = 0;
// 循環(huán)變量
String answer = "";
do {
//2. 接收用戶輸入的數(shù)據(jù)
boolean flag;
do {
// 接收人類出拳
System.out.print("選擇: ");
playInput = input.nextInt();
//判斷人類輸入的是否正確
if(playInput>3 || playInput<0) {
System.out.println("輸入選擇錯(cuò)誤 ");
flag = false;
}else {
flag = true;
}
}while(!flag);
// 判斷人出拳
switch(playInput) {
case 1:
System.out.println("你出拳為: 石頭");
break;
case 2:
System.out.println("你出拳為: 剪刀");
break;
case 3:
System.out.println("你出拳為: 布");
break;
}
//3. 電腦隨機(jī)產(chǎn)生數(shù)字
int comOutput = (int)(Math.random()*3)+1;
// 判段電腦的輸入老確定出拳
switch(comOutput) {
case 1:
System.out.println("電腦出拳為: 石頭");
break;
case 2:
System.out.println("電腦你出拳為: 剪刀");
break;
case 3:
System.out.println("電腦你出拳為: 布");
break;
}
// 4.判斷勝負(fù)
if(comOutput == 1 && playInput == 3 || comOutput == 2 && playInput == 1 || comOutput == 3 && playInput == 2) {
System.out.println("你贏了");
personScore++;
}else if(comOutput == playInput) {
System.out.println("打平");
}else{
System.out.println("你輸了");
computerScore++;
}
// 累計(jì)對(duì)戰(zhàn)次數(shù)
count++;
System.out.print("\n是否繼續(xù)(y/n) : ");
answer = input.next();
}while("y".equals(answer));
//5.統(tǒng)計(jì)勝負(fù)
System.out.println("\n你 VS 計(jì)算機(jī)");
System.out.println("對(duì)戰(zhàn)次數(shù): "+ count + "回合");
System.out.println("雙方積分為: "+personScore + "VS"+ computerScore);
// 判斷總勝負(fù)
if(personScore > computerScore) {
System.out.println("恭喜你贏了本回合");
}else if(personScore == computerScore) {
System.out.println("雙方打平");
}else {
System.out.println("電腦獲勝");
}
// 游戲結(jié)束
System.out.println("Game Over");
// 結(jié)束輸入
input.close();
}
}
Java 猜拳游戲 基礎(chǔ)版本
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 一些不一樣的規(guī)范 Kotlin不需要在語句末尾加;分號(hào) Kotlin的變量都是要初始化的,而Java則不需要 Ko...
- 前言 看到標(biāo)題可能有人要笑我,用Java寫游戲?沒辦法,畢竟無論學(xué)什么語言我們都希望它能做出好玩有趣的應(yīng)用。對(duì)于初...
- 實(shí)踐是最好的老師,它能教會(huì)我們許多光“看”書而看不到的知識(shí)。 內(nèi)容: “石頭、剪刀、布”是一種常見的猜拳游戲。游戲...
- Java語言概述 1詹姆斯·高斯林(James Gosling)1977年獲得了加拿大卡爾加里大 學(xué)計(jì)算機(jī)科學(xué)學(xué)士...
- 很多框架(比如 Dubbo)需要兼容多個(gè) JDK 版本,開發(fā)、測試時(shí)需要不斷切換版本,通過 JAVA_HOME手動(dòng)...