今天給大家分享一個(gè)Java工程人機(jī)猜拳
以下代碼僅供參考
package com.company;
import java.util.Scanner;
public class Game {
Scanner sc = new Scanner(System.in);
User User;//用戶玩家
Computer Computer;//電腦玩家
int count;//對(duì)戰(zhàn)次數(shù)
int score;//分?jǐn)?shù)
//游戲類的初始化方法
public void initial(){
//創(chuàng)建用戶對(duì)象
User= new User();
//創(chuàng)建電腦對(duì)象
Computer = new Computer();
//初始化對(duì)戰(zhàn)次數(shù)為0
count = 0;
}
//開(kāi)始游戲的方法
public void startGame(){
System.out.println("--------------------歡迎進(jìn)入游戲世界-----------------------\n\n");
System.out.println("\t\t*******************");
System.out.println("\t\t** 猜拳壳繁,開(kāi)始 **");
System.out.println("\t\t*******************\n\n");
System.out.println("出拳規(guī)則:1.石頭 2.剪刀 3.布");
System.out.print("請(qǐng)選著對(duì)方角色(1:劉備 2:孫權(quán) 3:曹操)");
int choose = sc.nextInt();//接收用戶選擇的角色
switch(choose){
case 1:
Computer.computerName = "劉備";//把值賦給電腦類的name
System.out.println("你選擇了"+Computer.computerName+"對(duì)戰(zhàn)");
break;
case 2:
Computer.computerName = "孫權(quán)";
System.out.println("你選擇了"+Computer.computerName+"對(duì)戰(zhàn)");
break;
case 3:
Computer.computerName = "曹操";
System.out.println("你選擇了"+Computer.computerName+"對(duì)戰(zhàn)");
break;
default:
System.out.println("輸入有誤券犁!");
break;
}
System.out.println("請(qǐng)輸入你的姓名:");
User.userName = sc.next();
System.out.println("要開(kāi)始嗎(y/n)");
String con = sc.next();
if(con.equals("n")){//如果不開(kāi)始輸入"n"的話 就提示退出
System.out.println("系統(tǒng)退出讹堤!");
}
int perFist;//用戶出的拳
int comFist;//計(jì)算機(jī)出的拳
while(con.equals("y")){
//出拳
perFist = User.showFirst();//調(diào)用用戶類的方法
comFist = Computer.chuQuan();//調(diào)用電腦類的方法
//裁決
if((perFist == 1 && comFist == 1) ||
(perFist == 2 && comFist == 2) ||
(perFist == 3 && comFist == 3)){
System.out.println("結(jié)果:和局,真衰浅侨!");//平局
}else if((perFist == 1 && comFist == 3) ||
(perFist == 2 && comFist == 1) ||
(perFist == 3 && comFist == 2)){
System.out.println("You lost!");//用戶輸了
System.out.println("結(jié)果:你被KO");
Computer.score++;//記錄電腦贏的分?jǐn)?shù)
}else{
System.out.println("You win!");//用戶贏了
System.out.println("結(jié)果:恭喜,你贏了!");
User.score++;//記錄用戶贏的分?jǐn)?shù)
}
count++;//對(duì)戰(zhàn)次數(shù)依次加一
System.out.println("是否進(jìn)入下一輪(y/n)");
con = sc.next();
}
//如果結(jié)束 則顯示最后的結(jié)果
showResult();
}
//顯示最后的輸出結(jié)果
public void showResult(){
System.out.println("------------------------------------------------");
System.out.println(Computer.computerName+"\tVS\t"+User.userName);
System.out.println("對(duì)戰(zhàn)次數(shù):"+count);
System.out.println("姓名"+"\t"+"積分");
System.out.println(User.userName+"\t"+User.score);
System.out.println(Computer.computerName+"\t"+Computer.score);
if(User.score>Computer.score){//如果用戶的分?jǐn)?shù)大于電腦的分?jǐn)?shù) 則用戶贏
System.out.println(User.userName+"贏了抵屿!");
}else{
System.out.println(Computer.computerName+"贏了聚唐!");
}
System.out.println("-------------------------------------------------");
}
}
然后在創(chuàng)建一個(gè)項(xiàng)目
package com.company;
import java.util.Scanner;
public class User {
String userName;//用戶名
int score;//積分
public int showFirst() {
System.out.print("請(qǐng)出拳:1.剪刀2.石頭3.布(輸入相應(yīng)的數(shù)字):");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch(choice) {
case 1:
System.out.println("你出拳:石頭");
break;
case 2:
System.out.println("你出拳:剪刀");
break;
case 3:
System.out.println("你出拳:布");
break;
default:
System.out.println("輸入錯(cuò)誤!");
break;
}
return choice;
}
}
創(chuàng)建方法
package com.company;
import java.util.Scanner;
public class Computer {
String computerName;//電腦名
int score;//電腦積分
public int chuQuan() {
int random =(int)(Math.random()*3)+1;
switch(random) {
case 1:
System.out.println(computerName+"出拳:石頭");
break;
case 2:
System.out.println(computerName+"出拳:剪刀");
break;
case 3:
System.out.println(computerName+"出拳:布");
break;
default:
System.out.println("輸入有錯(cuò)丐重,請(qǐng)重新輸入:");
break;
}
return random;
}
}
然后添加測(cè)試代碼
package com.company;
public class Main {
public static void main(String[] args) {
Game g = new Game();
g.initial();//調(diào)用初始化方法
g.startGame();//調(diào)用開(kāi)始游戲的方法
}
}