gpt4 book ai didi

java - 如何将二维数组与其他类一起使用

转载 作者:行者123 更新时间:2023-12-01 10:49:39 24 4
gpt4 key购买 nike

我有一个作业,我想知道如何在另一个类中使用 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 天才对我开始有什么建议吗?

谢谢!

最佳答案

这是你的主要方法,你只需要用这个更新你的主要方法,

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


}

已编辑

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;

}

关于java - 如何将二维数组与其他类一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33987149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com