如何将2D数组与其他类一起使用

时间:2015-11-29 19:38:01

标签: java arrays

我有一个作业,我想知道如何将2D数组用于另一个类,我有一个名为Die的类,如下所示:

public class Die
{
   private final int MAX = 6;  // maximum face value

   private int faceValue;  // current value showing on the die

   public Die()
   {
      faceValue = 1;
   }


   public int roll()
   {
      faceValue = (int)(Math.random() * MAX) + 1;

      return faceValue;
   }


   public void setFaceValue(int value)
   {
      faceValue = value;
   }


   public int getFaceValue()
   {
     return faceValue;
   }


   public String toString()
   {
      String result = Integer.toString(faceValue);

      return result;
   }
}

现在在主要方法中我必须执行以下操作

enter image description here

我完成了所有其他部分,我似乎无法弄清楚这一部分。

我当前的代码(未开始此部分)位于

之下
import java.util.Arrays;
import java.util.Scanner;

class ASgn8 
{

    public static void main(String[] args)
    {


    Scanner scan = new Scanner(System.in);

    System.out.print("How many players? ");
    int playerCount = scan.nextInt();
    scan.nextLine();

    String[] playerNames = new String[playerCount];
    int again = 1;

    for(int i = 0; i < playerCount; i++)
    {
        System.out.print("What is your name: ");
        playerNames[i] = scan.nextLine();
    }

    int randomNum = (int)(Math.random() * (30-10)) +10;


    }   
 }

你们这些java天才对我有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

这是您的主要方法,您只需要使用此方法更新主要方法

    public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);

            System.out.print("How many players? ");
            int playerCount = scan.nextInt();
            scan.nextLine();

            HashMap<String, ArrayList<Die>> hashMap = new  HashMap<String, ArrayList<Die>>();
            int again = 1;

            for(int i = 0; i < playerCount; i++)
            {
                System.out.print("What is your name: ");
                hashMap.put(scan.nextLine(),new ArrayList<Die>());
            }

            for(String key : hashMap.keySet()){
                System.out.println(key + "'s turn....");
                Die d = new Die();
                System.out.println("Rolled : " + d.roll()) ;
                hashMap.get(key).add(d);
                System.out.println("Want More (Yes/No) ???");
                String choice = scan.next();
                while(choice != null && choice.equalsIgnoreCase("YES")){
                    if(hashMap.get(key).size()>4){System.out.println("Sorry, Maximum 5-Try you can...!!!");break;}
                    Die dd = new Die();
                    System.out.println("Rolled : " + dd.roll()) ;
                    hashMap.get(key).add(dd);
                    System.out.println("Want More (Yes/No) ???");
                    choice = scan.next();
                }
            }

            for(String key : hashMap.keySet()){
                System.out.println(key + " - " + hashMap.get(key));
            }


        }

<强> EDITED

public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);

            System.out.print("How many players? ");
            int playerCount = scan.nextInt(); // get number of participant player...
            scan.nextLine();

            Die[] tempDie = new Die[5]; // temporary purpose
            Die[][] finalDie = new Die[5][]; // final array in which all rolled dies stores...

            String [] playerName = new String[playerCount]; // stores player name

            int totalRollDie = 0; // keep track number of user hash rolled dies...

            for(int i = 0; i < playerCount; i++) // get all player name from command prompt...
            {
                System.out.print("What is your name: ");
                String plyrName = scan.nextLine();
                playerName[i] = plyrName;
            }

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

                System.out.println(playerName[i] + "'s turn....");
                    totalRollDie = 0;
                    Die d = new Die();
                    System.out.println("Rolled : " + d.roll()) ;
                    tempDie[totalRollDie] = d; 
                    totalRollDie++;
                    System.out.println("Want More (Yes/No) ???");
                    String choice = scan.next();

                        while(choice != null && choice.equalsIgnoreCase("YES")){
                            if(totalRollDie < 5){ // if user want one more time to roll die then first check whether alread user has rolled 5-time or not.
                            Die dd = new Die();
                            System.out.println("Rolled : " + dd.roll()) ; // rolled and print whatever value get..
                            tempDie[totalRollDie] = dd;
                            totalRollDie++;
                            System.out.println("Want More (Yes/No) ???");
                            choice = scan.next();
                        }
                    }

                    finalDie[i] = new Die[totalRollDie];
                    for(int var = 0 ; var < totalRollDie ; var++){
                        finalDie[i][var] = tempDie[var]; // store Die object into finalDie array which can random number for all user.. 
                    }
            }

            for(int i = 0 ;i < playerCount ; i++){ // finally print whatever user's roll value with all try...
                System.out.println(" --------- " + playerName[i] + " ------------ ");
                for(Die de : finalDie[i]){
                    System.out.println(de);
                }
            }

            tempDie = null;

        }