简体   繁体   中英

How can I display an array from a void method?

I am a beginner with java and am trying to make a game of Yahtzee and am required to take a random dice roll as an array from a void method. Could someone explain to me why this wont work?

import java.util.Arrays;

public class YatzeeGame {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] diceRolls = new int[5];
    diceRolls = throwDice(diceRolls);
    System.out.println(display(diceRolls));
}

public static void throwDice(int [] dice) {     
    int [] roll = {(int)(Math.random()*6+1),
            (int)(Math.random()*6+1),(int)(Math.random()*6+1),
            (int)(Math.random()*6+1),(int)(Math.random()*6+1),
            (int)(Math.random()*6+1)};
    dice = roll;
}

public static String display(int [] dice) {
    String str = Arrays.toString(dice);
    str = str.replace("[", "");
    str = str.replace("]", "");
    str = str.replace("," , " ");
    return str;
}

They want you to replace the array, which doesn't happen if you just assign it. Note that returning the array is still considered a better way. Extra tricky: in your existing code you make one array of size 5 and the other of size 6. Since you're calling it zahtzee we'll use 5.

public static void throwDice(int [] dice) {     
    for (int x = 0; x < 5; x++)
        dice[x] = (int)(Math.random()*6+1);
}

Explanation of why it's not working:

What you're trying to do: Change dice (the parameter you passed in) to equal to roll. Essentially, (if i'm not wrong here) you're trying to change diceRolls using throwDice.

What you're actually doing: You've passed in diceRolls and said "here, let's call it dice". Then, at the end of your function, you've essentially said "dice doesn't mean diceRolls anymore. dice now means roll". Which means that diceRolls still hasn't changed.

You need to change the actual values of dice instead of changing what dice is. eg:

public static void throwDice(int[] dice) {
    // change the actual values of dice, instead of changing dice
    dice[0] = (int) (Math.random() * 6 + 1);
    dice[1] = (int) (Math.random() * 6 + 1);
    dice[2] = (int) (Math.random() * 6 + 1);
    dice[3] = (int) (Math.random() * 6 + 1);
    dice[4] = (int) (Math.random() * 6 + 1);
}

There's quite a few things wrong in your code.

In the throwDice method, dice is a local variable, therefore changing it to roll , which is another local variable, doesn't affect anything outside that method.

Also your return type is void , so you cannot set any variable using the method.

You could have a method that returns an int[] :

public static int[] throwDice() {
    int[] roll = new int[6];
    for (int i = 0; i < 6; i++) {
        roll[i] = (int) (Math.random() * 6) + 1;
    }
    return roll;
}

Then use it like:

int[] diceRolls = throwDice();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM