使用java實(shí)現(xiàn)猜拳小游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)猜拳小游戲的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)下圖要求

public class User {
private String u_name;
private int u_score;
public User() {
super();
}
public User(String name, int score) {
super();
this.u_name = name;
this.u_score = score;
}
public String getName() {
return u_name;
}
public void setName(String name) {
this.u_name = name;
}
public int getScore() {
return u_score;
}
public void setScore(int score) {
this.u_score = score;
}
/**
* 出拳方法
* @param choice 選擇的數(shù)字代表出拳(1:石頭2:剪刀3:布)
* @return str 返回你所選擇的出拳
*/
public String chuQuan(int choice){
String str = "";
switch (choice) {
case 1:
str = "石頭";
break;
case 2:
str = "剪刀";
break;
case 3:
str = "布";
break;
default:
System.out.println("未知錯(cuò)誤");
break;
}
return str;
}
}

public class Computer {
private String c_name;
private int c_score;
public String getName() {
return c_name;
}
public void setName(String name) {
this.c_name = name;
}
public int getScore() {
return c_score;
}
public void setScore(int score) {
this.c_score = score;
}
/**
* 出拳方法
* @param choice 選擇的數(shù)字代表出拳(1:石頭2:剪刀3:布)
* @return str 返回你所選擇的出拳
*/
public String chuQuan(int choice){
String str = "";
switch (choice) {
case 1:
str = "石頭";
break;
case 2:
str = "剪刀";
break;
case 3:
str = "布";
break;
default:
System.out.println("未知錯(cuò)誤");
break;
}
return str;
}
}

import java.util.Scanner;
public class Game {
Scanner input = new Scanner(System.in);
private User user;
private Computer computer;
private int count;
private int c_score;
private int u_score;
//初始化方法
public void init(){
user = new User();
computer = new Computer();
System.out.println("-----------------歡迎進(jìn)入游戲世界------------------");
System.out.println("\t **************************");
System.out.println("\t\t** 猜拳,開(kāi)始 **");
System.out.println("\t **************************");
System.out.println();
System.out.println("出拳規(guī)則:1.石頭 2.剪刀 3.布");
System.out.print("請(qǐng)選擇對(duì)方角色:(1:曹操 2:孫權(quán) 3:劉備):");
int key = input.nextInt();
switch (key) {
case 1:
computer.setName("曹操");
break;
case 2:
computer.setName("孫權(quán)");
break;
case 3:
computer.setName("劉備");
break;
default:
System.out.println("非法輸入...");
break;
}
System.out.print("請(qǐng)輸入你的姓名:");
user.setName(input.next());
System.out.println(user.getName()+" VS "+computer.getName());
begin();
}
//是否開(kāi)始執(zhí)行 循環(huán)執(zhí)行直到輸入n結(jié)束
public void begin(){
System.out.print("要開(kāi)始嗎(y/n):");
// boolean falg = true;
String str = input.next();
if(str.equals("y")){
while(true){
score();
System.out.print("是否開(kāi)始下一輪:(y/n)");
String str1 = input.next();
count++;
if(str1.equals("y")){
}else{
// falg = false;
break;
}
}
}
show();
}
//人和機(jī)器出拳并判斷勝負(fù) 此處計(jì)算比賽次數(shù) 雙方得分
public void score(){
System.out.print("請(qǐng)出拳:");
int choice1 = input.nextInt();
String str1 = user.chuQuan(choice1);
int choice2 = (int)(Math.random()*3+1);
String str2 = computer.chuQuan(choice2);
System.out.println("你出拳"+str1);
System.out.println(computer.getName()+"出拳"+str2);
if(choice1 == choice2){
System.out.println("結(jié)果:平局");
}else if(choice2-choice1==-1||choice2-choice1==2){
System.out.println("結(jié)果:"+computer.getName()+"獲勝...");
c_score++;
computer.setScore(c_score);
}else if(choice1-choice2==-1||choice1-choice2==2){
System.out.println("結(jié)果:恭喜你,你獲勝...");
u_score++;
user.setScore(u_score);
}
}
//顯示比賽結(jié)果并比較得得出最后勝負(fù)
public void show(){
System.out.println("--------------------------------");
System.out.println(user.getName()+" VS "+computer.getName());
System.out.println("對(duì)戰(zhàn)次數(shù):"+count+"\n\n");
System.out.println("姓名\t得分");
System.out.println(user.getName()+"\t"+user.getScore());
System.out.println(computer.getName()+"\t"+computer.getScore()+"\n");
if(user.getScore()>computer.getScore()){
System.out.println("結(jié)果:恭喜恭喜");
}else if(user.getScore()<computer.getScore()){
System.out.println("結(jié)果:再接再厲");
}else{
System.out.println("結(jié)果:平局");
}
System.out.println("--------------------------------");
}
}
測(cè)試類
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Game game = new Game();
game.init();
}
}
這樣猜拳小游戲就實(shí)現(xiàn)了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解如何在Java項(xiàng)目中實(shí)現(xiàn)信號(hào)的連續(xù)接收
在Java項(xiàng)目中,信號(hào)的連續(xù)接收是一項(xiàng)重要的任務(wù),特別是在處理異步事件或者需要對(duì)外部事件做出響應(yīng)時(shí),本篇博客將介紹如何在Java項(xiàng)目中實(shí)現(xiàn)信號(hào)的連續(xù)接收,包括信號(hào)的監(jiān)聽(tīng)、處理和停止等步驟,需要的朋友可以參考下2023-11-11
Java基礎(chǔ)之位運(yùn)算知識(shí)總結(jié)
最近接觸到了java位運(yùn)算,之前對(duì)位運(yùn)算的了解僅僅停留在表現(xiàn)結(jié)果上,乘2除以2,對(duì)背后的原理并不了解,現(xiàn)在學(xué)習(xí)記錄一下,需要的朋友可以參考下2021-05-05
SpringBoot日志配置SLF4J和Logback的方法實(shí)現(xiàn)
日志記錄是不可或缺的一部分,本文主要介紹了SpringBoot日志配置SLF4J和Logback的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
java通過(guò)Idea遠(yuǎn)程一鍵部署springboot到Docker詳解
這篇文章主要介紹了java通過(guò)Idea遠(yuǎn)程一鍵部署springboot到Docker詳解,Idea是Java開(kāi)發(fā)利器,springboot是Java生態(tài)中最流行的微服務(wù)框架,docker是時(shí)下最火的容器技術(shù),那么它們結(jié)合在一起會(huì)產(chǎn)生什么化學(xué)反應(yīng)呢?的相關(guān)資料2019-06-06
利用Java實(shí)現(xiàn)簡(jiǎn)單的詞法分析器實(shí)例代碼
眾所周知編譯原理中的詞法分析算是很重要的一個(gè)部分,原理比較簡(jiǎn)單,不過(guò)網(wǎng)上大部分都是用C語(yǔ)言或者C++來(lái)編寫(xiě),因?yàn)樽罱趯W(xué)習(xí)Java,故用Java語(yǔ)言實(shí)現(xiàn)了簡(jiǎn)單的詞法分析器。感興趣的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12
詳細(xì)介紹Java阿里云的短信驗(yàn)證碼實(shí)現(xiàn)
這篇文章主要介紹了詳細(xì)介紹Java阿里云的短信驗(yàn)證碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
SpringCloud Config分布式配置中心使用教程介紹
springcloud config是一個(gè)解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個(gè)部分,server端提供配置文件的存儲(chǔ)、以接口的形式將配置文件的內(nèi)容提供出去,client端通過(guò)接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用2022-12-12
淺談Java中ThreadLocal內(nèi)存泄露的原因及處理方式
內(nèi)存泄漏就是我們申請(qǐng)了內(nèi)存,但是該內(nèi)存一直無(wú)法釋放,就會(huì)導(dǎo)致內(nèi)存溢出問(wèn)題,本文詳細(xì)的介紹了ThreadLocal內(nèi)存泄露的原因及處理方式,感興趣的可以了解一下2023-05-05
四步輕松搞定java web每天定時(shí)執(zhí)行任務(wù)
本篇文章主要介紹了四步輕松搞定java web每天定時(shí)執(zhí)行任務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

