本帖最后由 遗忘 于 2013-10-4 13:45 编辑
import java.util.*;
public class StartGuess {
/**
* 人机猜拳
* @param args
*/
public static void main(String[] args) {
Game game = new Game();
game.startGame();
}
}
class Person
{
/**
* 用户类
*/
String name = "匿名";
//int score = 0;
public int showFist(){
Scanner input = new Scanner(System.in);
System.out.print("请出拳:1.剪刀 2.石头 3. 布(输入相应的数字):");
int show = input.nextInt();
switch(show){
case 1 :
System.out.println(name +"出拳:剪刀");
break;
case 2 :
System.out.println(name +"出拳:石头");
break;
case 3 :
System.out.println(name +"出拳:布");
break;
}
return show;
}
}
class Computer
{
/**
* 计算机类
*/
String name = "匿名";
int score = 0;
public int showFist(){
int show = (int)( Math.random()*10)%3 + 1;
switch(show){
case 1 :
System.out.println(name +"出拳:剪刀");
break;
case 2 :
System.out.println(name +"出拳:石头");
break;
case 3 :
System.out.println(name +"出拳:布");
break;
}
return show;
}
}
class Game
{
/**
* 开始游戏
*/
Person person;
Computer computer ;
/**
* 初始化*/
public void initial(){
Person person = new Person(); //用户
Computer computer = new Computer(); //电脑
}
public void startGame(){
initial();
System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------");
System.out.println("\n\t\t******************");
System.out.println ("\t\t** 猜拳, 开始 **");
System.out.println ("\t\t******************");
System.out.println("\n出拳规则:1.剪刀 2.石头 3.布");
Scanner input = new Scanner(System.in);
System.out.print("请选择对战人物: 1.曹操 2.刘备 3.孙权 :");
int num = input.nextInt();
if(num == 1){
computer.name = "曹操";
}else if(num==2){
computer.name = "刘备";
}else if(num == 3){
computer.name = "孙权";
}
System.out.print("请输入姓名:");
person.name = input.next();
System.out.println(person.name+" VS "+computer.name+" 对战\n");
System.out.print("\n要开始吗?(y/n): ");
String con = input.next();
int perFist; //用户出的拳
int compFist; //计算机出的拳
if(con.equals("y")){
/*出拳*/
perFist = person.showFist();
compFist = computer.showFist();
/*裁决*/
if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
System.out.println("结果:和局,真衰!\n"); //平局
}else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
System.out.println("结果: 恭喜, 你赢了!"); //用户赢
}else{
System.out.println("结果说:^_^,你输了,真笨!\n"); //计算机赢
}
}
}
}
|