public?class?Game?{
private?Scanner?scan?=?new?Scanner(System.in);
private?String?rule[]?=?{"","剪刀","石頭","布"};
private?String?role[]?=?{"","劉備","孫權","曹操"};
private?Computer?computer;
private?User?user;
private?int?roundCount;
private?Game(){
roundCount?=?0;
}
public?static?void?main(String[]?args)?{
Game?game?=?new?Game();
game.start();
}
public?void?start(){
computer?=?new?Computer();
System.out.println("-?-?歡迎進入遊戲世界?-?-");
System.out.println("**?******************");
System.out.println("**?猜拳,開始?**");
System.out.println("**?******************");
System.out.println();
System.out.println("出拳規則:1.剪刀?2.石頭?3.布");
System.out.print("請選擇角色(1:劉備?2.孫權?3.曹操)");
int?userRole?=?scan.nextInt();
if(userRole?>=1?&&?userRole?<=3){
user?=?new?User(role[userRole]);
System.out.println();
System.out.print("要開始嗎?(y/n)?");
String?start?=?scan.next();
if(start.equals("y")){
round();
}
else?if(start.equals("n")){
}
}
}
private?void?round(){
roundCount?++;
System.out.println();
System.out.print("請出拳:1.剪刀?2.石頭?3.布?(輸入相應數字):?");
int?userRule?=?user.round();
if(userRule?>=?1?&&?userRule?<=?3){
int?computerRule?=?computer.round();
judge(userRule,computerRule);
System.out.println();
System.out.print("是否開始下壹輪(y/n):?");
String?next?=?scan.next();
if(next.equals("y")){
round();
}
else?if(next.equals("n")){
end();
}
}
}
private?void?judge(int?ur,int?cr){
System.out.println("妳出拳:"+?rule[ur]);
System.out.println("電腦出拳:"+?rule[cr]);
if(ur?==?cr){
System.out.println("結果:和局,真衰!嘿嘿,等著瞧吧!");
}
else?if((ur?==?1?&&?cr==?3)||
(ur?==?2?&&?cr?==?1)||
(ur?==?3?&&?cr?==?2)){
System.out.println("結果:妳贏了!");
user.win();
}
else{
System.out.println("結果:妳輸了!");
computer.win();
}
}
private?void?end(){
System.out.println("-?-------------------------------------");
System.out.println(user.getName()?+?"VS"?+?computer.getName());
System.out.println("對戰次數:"?+?roundCount);
if(computer.getWin()?==?user.getWin()){
System.out.println("結果:打成平手,下次再和妳壹分高下!");
}
else?if(computer.getWin()?>?user.getWin()){
System.out.println("結果:妳輸了!電腦贏了"?+?computer.getWin()+"次!");
}?
else{
System.out.println("結果:妳贏了!妳贏了"?+?user.getWin()+"次!");
}
System.out.println("-?-------------------------------------");
}
class?Computer{
private?int?win;
private?String?name;
public?Computer(){
win?=?0;
name?=?"匿名";
}
public?int?round(){
return?(int)(System.currentTimeMillis()?%?3)?+?1;//隨機返回1、2、3
}
public?String?getName(){
return?name;
}
public?void?win(){
win?++;
}
public?int?getWin(){
return?win;
}
}
class?User{
private?int?win;
private?String?name?=?"";
public?User(String?name){
this.name?=?name;
win?=?0;
}
public?int?round(){
return?scan.nextInt();
}
public?String?getName(){
return?name;
}
public?void?win(){
win?++;
}
public?int?getWin(){
return?win;
}
}
}
剛剛寫好的,應該滿足妳的題目要求。贏了和輸了的提示信息題目裏沒有是我自己隨便寫的。