为了账号安全,请及时绑定邮箱和手机立即绑定

帮忙看下调用其它类中的list方法为什么不成功

主函数

public class Main {
 public static void main(String[] args) {
  MakePuKe a1 = new MakePuKe();
  a1.createPuKe();
  a1.xiPai();
  PlayGame p1 =new PlayGame();
  p1.player1();
  p1.player2();
  //p1.faPai();
  p1.theEnd();
 }
}

user类  玩家类

import java.util.Comparator;
public class User extends PuKe implements Comparator<User>{
 private int ID;
 private String name;
 User(int ID,String name,String style, String number) {
  super(style, number);
  this.ID=ID;
  this.name=name;
 }
 public User() {}
 public int getID(){
  return ID;
 }
 public String name(){
  return name;
 }
 @Override
 public int compare(User o1, User o2) {
   return o1.getNumber().compareTo(o2.getNumber());
 }
}

MakePuKe类

import java.util.*;
public class MakePuKe {
 public List<PuKe> container;
 public Set<PuKe> shuffleContainer;
 {
  container = new ArrayList<PuKe>();
  shuffleContainer = new HashSet<PuKe>();
 }
 public void createPuKe(){
  System.out.println("********正在组建扑克********");
  String[] pukeStyle = {"黑桃","红桃","梅花","方片"};
  String[] pukeNumber = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
  for(String i:pukeStyle){
   for(String j:pukeNumber){
    PuKe a = new PuKe(i,j);
    container.add(a);
   }
  } 
  System.out.println("********扑克组建完成********");
  for(PuKe i:container){
   System.out.print(i.getStyle()+i.getNumber()+",");
  }
  System.out.println();
 }
 public void xiPai(){
  System.out.println("**********开始洗牌**********");
  for(int i = 0 ;i<container.size();i++){
   PuKe a = container.get(i);
   shuffleContainer.add(a);
   container.remove(i);
  }
  for(PuKe i : shuffleContainer){
   PuKe a = new PuKe(i.getStyle(),i.getNumber());
   container.add(a);
  }
  System.out.println("**********洗牌结束**********");
 }
}

最后一个实现比较手牌大小的类

import java.util.*;
public class PlayGame {
 public List<User> userList;
 {
  userList = new ArrayList<User>();
 }
 Scanner in = new Scanner(System.in);
 int newID1;
 String newName1;
 int newID2;
 String newName2;
 MakePuKe mpk = new MakePuKe();
 public void player1(){
  System.out.print("请输入玩家一ID:");
  try{
   newID1 = in.nextInt();
  }
  catch(Exception e){
   System.out.print("int类型ID");
   player1();
  }
  System.out.print("请输入玩家一姓名:");
  newName1 = in.next();
 }
 public void player2(){
  try{
   while(true){
    System.out.print("请输入玩家二ID:");
    newID2 = in.nextInt();
    if(newID2!=newID1){
     break;
    }
    else{
     System.out.print("姓名重复!");
    }
   }
  }
  catch(Exception e){
   System.out.print("int类型ID");
   player1();
  }
  System.out.print("请输入玩家二姓名:");
  newName2 = in.next();
 }
 public void faPai(){
  System.out.println("**********开始发牌**********");
  for(int i=0;i<4;i++){
   //PuKe a1 = mpk.container.get(i);
   User u1 = new User(newID1,newName1,mpk.container.get(i).getStyle(),mpk.container.get(i).getNumber());
   userList.add(u1);
   i++;
   PuKe a2 = mpk.container.get(i);
   User u2 = new User(newID2,newName2,a2.getStyle(),a2.getNumber());
   userList.add(u2);
  }
 }
 public void theEnd(){
  Collections.sort(userList, new User());
  String a1 = userList.get(0).getNumber();
  String a2 = userList.get(1).getNumber();
  int i = 0;
  int j = 0;
  if(a1.equals(a2)){
   switch(userList.get(0).getStyle()){
   case "黑桃":
    i=4;break;
   case "红桃":
    i=3;break;
   case "梅花":
    i=2;break;
   case "方片":
    i=1;break;
   }
   switch(userList.get(1).getStyle()){
   case "黑桃":
    j=4;break;
   case "红桃":
    j=3;break;
   case "梅花":
    j=2;break;
   case "方片":
    j=1;break;
   }
   if(i>j){
    System.out.println(userList.get(0).name()+"获胜");
   }
   else{
    System.out.println(userList.get(1).name()+"获胜");
   }
  }
  else{
   System.out.println(userList.get(0).name()+"获胜");
  }
 }
}


思路大致是
用List类型的集合container存放扑克
通过Set类型的集合shuffleContainer洗牌,并把洗牌的结果保存回container中(这两个集合在同一类下)
再建立一个List类型的集合userList,存放玩家id,name,手牌。然后排序,比较输出结果

程序还没写全,思路肯定不是很好。。。但是只能想到这么多了,执行会报错,求大神帮忙调试一下。若有其他好的思路,指点一二也是极好的。

正在回答

1 回答

有感叹号 的是修改的地方//!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Main类中:

public class Start {

public static void main(String[] args) {

// TODO Auto-generated method stub

MakePuKe a1 = new MakePuKe();

 a1.createPuKe();

 a1.xiPai();

 PlayGame p1 =new PlayGame();

 p1.player1();

 p1.player2();

 p1.faPai(a1);//!!!!!!!!!!!!!!!!!!!!!!!!

 p1.theEnd();

}

}

PlayGame类中:

 public void faPai(MakePuKe A){                    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 System.out.println("**********开始发牌**********");

 for(int i=0;i<4;i++){

 

  User u1 = new User(newID1,newName1,A.container.get(i).getStyle(),A.container.get(i).getNumber());//!!!!!!!!!!!!!!!!!!!!!!!!!

  userList.add(u1);

  i++;

 

  User u2 = new User(newID2,newName2,A.container.get(i).getStyle(),A.container.get(i).getNumber());//!!!!!!!!!!!!!!!!!!!!!!!!!!!

  userList.add(u2);

 }

}

public void theEnd(){

 Collections.sort(userList, new User());

 String a1 = userList.get(3).getNumber();     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 String a2 = userList.get(2).getNumber();    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 int i = 0;

 int j = 0;

 System.out.println("最后的牌为:");

 for(User U : userList)                   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11

 {

 System.out.println("玩家ID:"+U.getID()+"  牌为: "+ U.getNumber()+" "+U.getStyle() );

 }

 if(a1.equals(a2)){

  switch(userList.get(3).getStyle()){  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  case "黑桃":

   i=4;break;

  case "红桃":

   i=3;break;

  case "梅花":

   i=2;break;

  case "方片":

   i=1;break;

  }

  switch(userList.get(2).getStyle()){        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!111111

  case "黑桃":

   j=4;break;

  case "红桃":

   j=3;break;

  case "梅花":

   j=2;break;

  case "方片":

   j=1;break;

  }

  if(i>j){

   System.out.println("玩家ID:"+userList.get(3).getID()+"获胜");        //!!!!!!!!!!!!!!!!!!

  }

  else{

   System.out.println("玩家ID:" + userList.get(2).getID()+"获胜");        //!!!!!!!!!!!!!!!!!!!!!!!!!!!

  }

 }

 else{

  System.out.println("玩家ID:"+ userList.get(3).getID()+"获胜");   //!!!!!!!!!!!!!!!!!!!!!!!!!!!1111

 }

}








0 回复 有任何疑惑可以回复我~
#1

Goldberg 提问者

函数传进来呢?直接在本类中new为啥不行
2016-03-01 回复 有任何疑惑可以回复我~
#2

Goldberg 提问者

为何一定要在住函数把引用传进来呢?直接在本类中new为啥不行
2016-03-01 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

帮忙看下调用其它类中的list方法为什么不成功

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信