程式思路:

效果

程式設想
-
先顯示歡迎陳述句
-
錄用NPC與用戶的昵稱
-
讓用戶選擇自己要出的拳
-
顯示NPC的拳
-
判斷用戶和NPC的拳
-
積分計數
-
詢問用戶是否要退出
-
退出后判斷積分,輸出對應陳述句
代碼
我這里用的是 面向物件寫的
首先 搭建平臺
import java.util.Scanner;
?
public class Game {
//第幾局
int tamptime;
?
//定義陣列來儲存拳
String[] quan={"石頭","剪刀","布"};
?
//實體化用戶物件
user user = new user();
?
//實體化NPC物件
Npc Npc = new Npc();
?
//1.用戶物件
user username;
//2.NPC物件
Npc name;
public void showplayergame() {
//顯示歡迎陳述句
showplayer();
?
//初始化資料
initData();
?
?
//游戲回圈
System.out.println("請問要開始游戲嗎?(y/n)");
//掃描儀
Scanner input = new Scanner(System.in);
//定義變數來接受值
String start = input.next();
?
//游戲回圈
while (start.equals("y")) {
?
System.out.println("用戶開始出拳");
?
//定義變數接收用戶輸入的值
int userquan = user.showusergetquan();
//轉換用戶輸入的值
String getVel = getValue(userquan);
System.out.println(user.username+"輸入的是:"+getVel);
?
//接收亂數
int getnpcVel = Npc.showgetquan();
//轉換亂數
String getNpcVel = getValue(getnpcVel);
System.out.println(Npc.name+"出的是:"+getNpcVel);
?
//判斷; 接識訓傳值
//實體化裁判
test test = new test();
//回傳值 = 類名.方法名(用戶quan,Npcquan);
int res = test.CaiPan(userquan,getnpcVel);
if (res == 1){
//用戶加一份
user.scres++;
System.out.println("恭喜您贏了");
}else if (res == -1){
//npc加一分
Npc.scres++;
System.out.println("對不起您輸了");
}else{
System.out.println("平局");
}
?
//局數加一
tamptime++;
System.out.println("第"+tamptime+"局游戲結束");
?
//顯示積分
showshool();
?
//詢問用戶是否要繼續游戲
System.out.print("請問您要繼續游玩嗎(y/n):");
start = input.next();
?
}
//輸出結果
showresult();
?
}
public void showresult(){
//判斷
if (user.scres > Npc.scres){
System.out.println(user.username+"別走,在與我大戰三百回合!");
}else if(user.scres < Npc.scres){
System.out.println("哈哈哈,小子你不行,快回家多練幾年");
}else if (user.scres == Npc.scres){
System.out.println("勢均力敵,居然是平均,"+user.username+"你別走,再和我繼續大戰三百回合!");
}
}
?
//顯示積分
public void showshool(){
//輸出用戶積分
System.out.println(user.username+"的積分是:"+user.scres);
//輸出NPC積分
System.out.println(Npc.name+"的積分是:"+Npc.scres);
}
?
//更改資料
public String getValue(int AAquan){
//把用戶數輸入的1 2 3 改成 陣列下標
int dix = AAquan-1;
return quan[dix];
?
}
?
?
//錄用 玩家 NPC姓名
public void initData(){
//新建掃描儀
Scanner input = new Scanner(System.in);
?
//請輸入用戶昵稱
System.out.print("請輸入您的昵稱:");
?
//錄入玩家姓名
user.username = input.next();
?
// 請輸入NPC的姓名
System.out.print("請輸入NPC的名字:");
?
//錄入NPC姓名
Npc.name = input.next();
?
}
?
//歡迎陳述句與選單
public void showplayer(){
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * *");
System.out.println("歡迎來到猜拳小游戲!");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * *");
}
}
?
在寫用戶與NPC的類
用戶
import java.util.Scanner;
?
public class user {
//定義用戶姓名
String username;
//定義變數來接受用戶勝利的場數
int scres;
public int showusergetquan(){
System.out.println("請選擇出拳:1.石頭 2.剪刀 3.布");
//掃描儀
Scanner input = new Scanner(System.in);
//接收用戶輸入
return input.nextInt();
}
}
?
NPC
import java.util.Random;
?
public class Npc {
//定義 NPC 姓名
String name;
//定義變數來接受 NPC 勝利的場數
int scres;
//讓NPC隨機出拳
public int showgetquan(){
//定義亂數物件
Random random = new Random();
//定義一個int 型別的值來接收 數
return random.nextInt(3)+1;
}
}
?
用戶和NPC撰寫完成后再寫裁判的類
裁判
public class test {
//-判斷
public int CaiPan(int userquan , int Npcquan) {
//定義一個初始值
int res = 0;
// 1.石頭 2. 剪刀 3. 布
if (userquan == 1 && Npcquan ==2 || userquan == 2 && Npcquan == 3|| userquan == 3 && Npcquan == 1 ){
//這里不可以為++;
res = 1;
}else if(Npcquan == 1 && userquan ==2 || Npcquan == 2 && userquan == 3|| Npcquan == 3 && userquan == 1 ){
res = -1;
}
return res;
}
}
?
用戶,NPC和平臺都寫完后再寫開始了
直接呼叫就可以了
開始
public class kaishi {
public static void main(String[] args) {
//實體化物件
Game Game = new Game();
Game.showplayergame();
}
}
?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/299765.html
標籤:其他
