如何使用与其他类二维数组 [英] How to use 2D arrays with other classes

查看:111
本文介绍了如何使用与其他类二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务,我不知道我怎么会去使用二维阵列,另一个类,我有一类称为模具,看起来像这样:

I have an assignment, I was wondering how I could go about using 2D arrays with another class, I have a class called Die that looks like this:

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;
   }
}

现在在main方法我必须做以下

Now in a main method i have to do the following

在这里输入的形象描述

我所有的其他部分做的,我只是不能似乎要弄清楚这一部分。

I have all the other parts done, I just cant seem to figure out this part.

我目前的code(未启动这部分)低于

My current code(Not started this part) is below

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天才有什么建议,我开始?

Do any of you java geniuses have any advice for me to begin?

谢谢!

推荐答案

下面是你的主要方法,你只需要这一个更新您的主要方法,

Here is your main method, you just need to update your main method with this one,

    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;

        }

这篇关于如何使用与其他类二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆