[java]代码库
package com.getGame;
import java.util.Scanner;
public class RandomGame {
public final static int jiandao=1;
public final static int shitou=2;
public final static int bu=3;
public final static String JIANDAO="剪刀";
public final static String SHITOU="石头";
public final static String BU="布";
boolean panDuan=true;
public void open(){
while(panDuan){
int computer=(int)(Math.random()*3+1);
Scanner scanner=new Scanner(System.in);
System.out.println("开始游戏\n请出拳:");
String b=scanner.next();
int person=String(b);
if(person==0){
System.out.println("你出的什么,电脑无法验证,重新输入");
System.out.println("是否继续开始游戏:是/不");
String colse=scanner.next();
if(colse.equalsIgnoreCase("是")){
panDuan=true;
continue;
}else if(colse.equalsIgnoreCase("不")){
panDuan=false;
System.out.println("结束游戏");
continue;
}else{
panDuan=false;
System.out.println("游戏结束");
continue;
}
}
both(person,computer);
System.out.println("是否继续开始游戏");
String start=scanner.next();
if(start.equalsIgnoreCase("是")){
panDuan=true;
continue;
}else if(start.equalsIgnoreCase("不")){
panDuan=false;
System.out.println("结束游戏");
continue;
}else{
panDuan=false;
System.out.println("游戏结束");
continue;
}
}
}
private void both(int person, int computer) {
if(person==computer){
System.out.println("我出的是:"+daKai(person)+",电脑出的是"+daKai(computer)+",是平局\t");
}else if(person==jiandao&&computer==shitou||person==shitou&&computer==bu||person==bu&&computer==jiandao){
System.out.println("我出的是:"+daKai(person)+",电脑出的是"+daKai(computer)+",我输了\t");
}else{
System.out.println("我出的是:"+daKai(person)+",电脑出的是"+daKai(computer)+",我赢了\t");
}
}
private String daKai(int person) {
String c="";
switch(person){
case jiandao:
c=JIANDAO;
break;
case shitou:
c=SHITOU;
break;
case bu:
c=BU;
break;
}
return c;
}
private int String(String b) {
if(b.equals(JIANDAO)){
return jiandao;
}else if(b.equals(SHITOU)){
return shitou;
}else if(b.equals(BU)){
return bu;
}
return 0;
}
}
package com.getGame;
public class Main {
public static void main(String args[]){
RandomGame randomGame=new RandomGame();
randomGame.open();
}
}
by: 发表于:2017-08-04 14:55:12 顶(0) | 踩(0) 回复
??
回复评论