作业要求:

db7a3bb099901a43209d18dcd052ef74.png

79db224233aa627ef8256cbe72f017f7.png

25ba69c3c1bb29a2733df284ca24c1fa.png

820cdc6f27183ae6b4a5458c1a268f99.png

e2a5d48c66ef6f3619adfc7cfae91572.png

4a925e125eaf6ba24188cef2a0be5164.png

3b06356d06d13a3ca65db17cba040cd9.png

f0e2a189bfee29497b99b6504b6e5336.png

我的代码:

package day20181119;

/**

* 猜拳游戏

* @author Administrator

* @version1.0

*/

import java.util.Scanner;

public class FingerGuessing {

String chName;

String name;

int i;

int ch1;

int Score1;

int ch2;

int Score2;

public void showLoginMenu(){

System.out.println("--------------欢迎来到游戏世界------------");

System.out.println("\t***********************");

System.out.println("\t *****猜拳,开始*****");

System.out.println("\t***********************");

System.out.println("\n出拳规则:1.剪刀2.石头3.布");

System.out.print("请选择对方角色:(1.刘备2.孙权3.曹操)");

Scanner input=new Scanner (System.in);

int ch=input.nextInt();

if(ch==1){

chName="刘备";

showStartGame();

}else if(ch==2){

chName="孙权";

showStartGame();

}else if(ch==3){

chName="曹操";

showStartGame();

}else{

System.out.println("您的输入有误,请重新输入!");

showLoginMenu();

}

}

public void showStartGame(){

Scanner input=new Scanner(System.in);

System.out.print("请输入您的姓名:");

name=input.nextLine();

System.out.println(name+"VS"+chName+"对战!");

showStart();

}

public void showStart(){

Scanner input=new Scanner(System.in);

i=i+1;

System.out.print("\n要开始第"+i+"轮吗?(Y/N)");

String ch=input.nextLine();

if(ch.equals("Y")){

showFist();

}else{

showResult2();

}

}

public void showFist(){

System.out.print("请出拳:1.剪刀2.石头3.布(输入相应数字):");

Scanner input=new Scanner(System.in);

ch1=input.nextInt();

if(ch1==1){

System.out.println("你出拳:剪刀");

showComputer();

}else if(ch1==2){

System.out.println("你出拳:石头");

showComputer();

}else if(ch1==3){

System.out.println("你出拳:布");

showComputer();

}

}

public void showComputer(){

ch2=(int)(Math.random()*3);

if(ch2==0){

System.out.println(chName+"出拳:剪刀");

showResult();

}else if(ch2==1){

System.out.println(chName+"出拳:石头");

showResult();

}else if(ch2==2){

System.out.println(chName+"出拳:布");

showResult();

}

}

public void showResult(){

if(ch1==1&&ch2==1||ch1==2&&ch2==2||ch1==3&&ch2==0){

System.out.println("结果是:^_^,你输了,下次加油啊!");

Score2++;

showStart();

}else if(ch1==1&&ch2==2||ch1==2&&ch2==0||ch1==3&&ch2==1){

System.out.println("结果是:恭喜,你赢了!");

Score1++;

showStart();

}else if(ch1==1&&ch2==0||ch1==2&&ch2==1||ch1==3&&ch2==2){

System.out.println("结果是:和局,真帅!");

showStart();

}

}

public void showResult2(){

System.out.println("------------------------------------");

System.out.println(name+"VS"+chName);

System.out.println("对战次数:"+(i-1));

System.out.println("姓名\t得分");

System.out.println(name+"\t"+Score1);

System.out.println(chName+"\t"+Score2);

if(Score1>Score2){

System.out.print("结果:恭喜恭喜!");

}else if(Score1

System.out.println("结果:下次再加油哦!");

}else if(Score1==Score2){

System.out.println("真是皆大欢喜的结局呢!");

}

System.out.println("------------------------------------");

}

}

-------------------------------------------------------------------------------------

package day20181119;

public class TestFingerGuessing {

public static void main(String[] args) {

FingerGuessing game=new FingerGuessing();

game.showLoginMenu();

}

}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐