///用户类
package Phase;
import java.util.*;
public class user {
   String name;
   int score;
   
//   public static void main(String[]args){
// user d=new user();
// d.showFist();
//   }
       
       public int showFist(){
       System.out.println("请出拳:1. 剪刀 2. 石头 3. 布(输入相应的数字):");
       Scanner input=new Scanner (System.in);    
   int num=input.nextInt();
   switch(num){
   case 1:
   System.out.println("你出的拳:剪刀");
   break;
   case 2:
   System.out.println("你出的拳:石头");
   break;
   case 3:
   System.out.println("你出的拳:布");
       break;   
   }
   return num;    
   }      
}
//电脑类
package Phase;
import java.util.*;
public class computer {
String name;
int score;
public int a(){
int num=(int)(Math.random()*4+1);
switch(num){
case 1:
System.out.println("电脑出拳:剪刀");
break;
case 2:
System.out.println("电脑出拳:石头");
        break;
case 3:
System.out.println("电脑出拳:布");
    break;
}
  return num;
}
}//游戏类package Phase;
import java.util.*;
public class game {
Scanner input=new Scanner(System.in);
   /*
    * 实列化user类和computer类
    */
   user one=new user();
   computer two=new computer();
    
   /*
    * 定义类的属性
    */
   String person,computer;
   int count,dogfall;//声明平局变量
   //赋值方法
   public  void evaluate(){
 person=" ";
 computer="匿名";
 count=0;
 dogfall=0;
   }
    /* 分析结果只有三种(电脑胜、平局、玩家赢)
    * 断判每次情况并保成在一个全局变量中,
    * int型的carlcResult返回这个变量
    */
   public int calcResult(){
   if((one.showFist()==1&&two.a()==2)||
   (one.showFist()==2&&two.a()==3)||(one.showFist()==1&&two.a()==1)){
   System.out.println(computer+"胜");
   count=1;
   two.score=1;
   }else if(two.a()==one.showFist()){
   System.out.println("平局");
   count=2;    
   }else{
   System.out.println(person+"胜");
   count=3;
   }
   return count;
   }
   /*
    * 定义显示方法
    * 调用前一个方法显示相应的结果
    */
   public void xs(){
   if(calcResult()==1){
   System.out.println("结果:呵呵,笨笨,下次加油啊!");
   two.score=1;
   }else if(calcResult()==2){
   System.out.println("结果:打成平手,下次再和一分高下!");
   dogfall=1;
   }else if(calcResult()==3){
   System.out.println("结果:恭喜恭喜!");
   one.score=1;
   }
   }
   //开始方法
   public  void start(){
   int degree=0; 
   System.out.print("要开始吗?(y/n)");
   String answer=input.next();
   while(answer.equals("y")){
     degree=degree+1;
   one.showFist();
     two.a();
     xs();
     System.out.println("是否开始下一轮(y/n)");
     answer=input.next();
       }
   }
   /*
    * 程序主入口
    */
   public static void main(String[]args){
   game three=new game(); 
   Scanner input=new Scanner(System.in);
   System.out.println("--------------------欢迎进入游戏世界--------------------" +
    "\n\n\t\t  ****************\n\t\t  **  猜拳  开始  **\n\t\t  ****************");
   System.out.println("出拳规则:1.剪刀 2.石头 3.布");
   System.out.print("请选择一个角色(1:刘备 2:石头 3:曹操:):");
   int b=input.nextInt();
   switch(b){
   case 1:
   three.person="刘备";
       break;   
   case 2:
   three.person="孙权";
   break;
   case 3:
   three.person="曹操";
   break;
   }   
   three.evaluate();
   three.start();
   System.out.println("------------------------------------------------------");
   System.out.println(three.person+"vs"+three.computer);
//    System.out.println("对战次数:"+)
   three.xs();
   }
}

解决方案 »

  1.   

    package Phase;import java.util.*;public class game {
    Scanner input = new Scanner(System.in);
    /*
     * 实列化user类和computer类
     */
    user one = new user();
    computer two = new computer(); /*
     * 定义类的属性
     */
    String person, computer;
    int count, dogfall;// 声明平局变量 // 赋值方法
    public void evaluate() {
    person = " ";
    computer = "匿名";
    count = 0;
    dogfall = 0;
    } /*
     * 分析结果只有三种(电脑胜、平局、玩家赢) 断判每次情况并保成在一个全局变量中, int型的carlcResult返回这个变量
     */
    public int calcResult() {
    if ((one.showFist() == 1 && two.a() == 2)
    || (one.showFist() == 2 && two.a() == 3)
    || (one.showFist() == 1 && two.a() == 1)) {
    System.out.println(computer + "胜");
    count = 1;
    two.score = 1;
    } else if (two.a() == one.showFist()) {
    System.out.println("平局");
    count = 2;
    } else {
    System.out.println(person + "胜");
    count = 3;
    }
    return count;
    } /*
     * 定义显示方法 调用前一个方法显示相应的结果
     */
    public void xs() {
    if (calcResult() == 1) {
    System.out.println("结果:呵呵,笨笨,下次加油啊!");
    two.score = 1;
    } else if (calcResult() == 2) {
    System.out.println("结果:打成平手,下次再和一分高下!");
    dogfall = 1;
    } else if (calcResult() == 3) {
    System.out.println("结果:恭喜恭喜!");
    one.score = 1;
    }
    } // 开始方法
    public void start() {
    int degree = 0;
    System.out.print("要开始吗?(y/n)");
    String answer = input.next();
    while (answer.equals("y")) {
    degree = degree + 1;
    one.showFist();
    two.a();
    xs();
    System.out.println("是否开始下一轮(y/n)");
    answer = input.next();
    }
    } /*
     * 程序主入口
     */
    public static void main(String[] args) {
    game three = new game();
    Scanner input = new Scanner(System.in);
    System.out
    .println("--------------------欢迎进入游戏世界--------------------"
    + "\n\n\t\t  ****************\n\t\t  **  猜拳  开始  **\n\t\t  ****************");
    System.out.println("出拳规则:1.剪刀 2.石头 3.布");
    System.out.print("请选择一个角色(1:刘备 2:石头 3:曹操:):");
    int b = input.nextInt();
    switch (b) {
    case 1:
    three.person = "刘备";
    break;
    case 2:
    three.person = "孙权";
    break;
    case 3:
    three.person = "曹操";
    break;
    }
    three.evaluate();
    three.start();
    System.out
    .println("------------------------------------------------------");
    System.out.println(three.person + "vs" + three.computer);
    // System.out.println("对战次数:"+)
    three.xs();
    }
    }
      

  2.   

     if ((one.showFist() == 1 && two.a() == 2)
                    || (one.showFist() == 2 && two.a() == 3)
                    || (one.showFist() == 1 && two.a() == 1)) 
    这里是不是错了,改为
     if ((one.showFist() == 1 && two.a() == 2)
                    || (one.showFist() == 2 && two.a() == 3)
                    || (one.showFist() == 1 && two.a() == 3))其实直接
     if ((one.showFist() <two.a() ))