简体   繁体   中英

How can I fix this variable error in my 2D Array program?

I am writing a penny pitch program in java and it is a carnival game. Prizes are randomly generated on a board and 10 coins are thrown. In the "randSelector" method I have written, I am getting an error from "prize" because it says I cant return a String to String []. Any Suggestions? This is my code:

import java.util.Arrays;
import java.util.Random;

public class PennyPitch {

public static void main(String[] args) {
    // TODO Auto-generated method stub
        Random rand = new Random();
        String[][] board = new String[25][25];
        int puzzle = 0;
        int poster = 0;
        int doll = 0;
        int ball = 0;
        int game = 0;
        int misses = 0;
        
        //Create empty board
        Arrays.fill(board, "[        ]");
        
        String[] prizes = {"[ puzzle ]", "[ poster ]", "[  doll  ]", "[  ball  ]", "[  game  ]"};
        for (int i = 0; i < prizes.length; i++) {
            randSelecter(board, prizes[i], rand);
        }
        //Simulates 10 pennies thrown
        for ( int i = 0; i < 10; i++) {
            for ( int j = 0; j < 10; j++){
            int box = rand.nextInt(24);
            if (board[box][j] == "[ puzzle ]"){
                
                puzzle += 1;
            } else if (board[box][j] == "[ poster ]"){
                poster += 1;
            } else if (board[box][j] == "[  doll  ]"){
                doll += 1;
            } else if (board[box][j] == "[  ball  ]"){
                ball += 1;
            } else if (board[box][j] == "[  game  ]"){
                game += 1;
            }else {
                misses += 1;
            }
        }

        System.out.println("Welcome to Penny Pitch!");
        System.out.println("Land on three of a kind and win the prize!");
        System.out.println("You've thrown 10 pennies on the board below,\n");

        //Prints 5 x 5 board
        System.out.println(board[0][0] + board[1][1] + board[2][2] + board[3][3] + board[4][4]);
        System.out.println(board[5][5] + board[6][6] + board[7][7] + board[8][8] + board[9][9]);
        System.out.println(board[10][10] + board[11][11] + board[12][12] + board[13][13] + board[14][14]);
        System.out.println(board[15][15] + board[16][16] + board[17][17] + board[18][18] + board[19][19]);
        System.out.println(board[20][20] + board[21][21] + board[22][22] + board[23][23] + board[24][24] + "\n");

        System.out.println("Here is your outcome:" );
        System.out.println("Puzzle: " + puzzle );
        System.out.println("Poster: " + poster );
        System.out.println("Doll: " + doll );
        System.out.println("Ball: " + ball );
        System.out.println("Game: " + game );
        System.out.println("Misses: " + misses );
        
        //Checks for a win
        if ( puzzle == 3){
            System.out.println("You won a puzzle!");
        } else if ( poster == 3) {
            System.out.println("You won a poster!");
        } else if ( doll == 3) {
            System.out.println("You won a doll!");
        } else if ( ball == 3) {
            System.out.println("You won a ball!");
        } else if ( game == 3) {
            System.out.println("You won a game!");
        } else {
            System.out.println("Sorry, you lost.");
        }

    }
}
    //Randomly place prizes
    public static String[] randSelecter(String[][] board, String prize, Random rand) {
        for (int i = 0; i < 3; i++) {
            int pick = rand.nextInt(24);
            int anotherNum = rand.nextInt(24);
            if (board[pick][anotherNum] != "[        ]" && board[pick + 1][anotherNum+1] == "[        ]"  && pick != 24) {
                board[pick + 1] = prize;
            } else if (board[pick][anotherNum] != "[        ]" && pick != 0 && board[pick - 1][anotherNum-1] == "[        ]") {
                board[pick - 1] = prize;
            } else if (board[pick][pick] != "[        ]") {
                for (int k = 0; k < board.length; ) {
                    if (board[k][anotherNum] == "[        ]") {
                        board[k][anotherNum] = prize;
                        break;
                    } else {
                        k++;
                    }
                }
            } else {
                board[pick] = prize;
            }
        }
        for(int x = 0;x<30;x++) {
            for(int p = 0;p<30;p++) {
                return board[x][p];
            }
        }
        
    }

}

Your return type in randSelecter is String[] but you're returning a String by doing return board[x][p] . Change your return type to String :

public static String randSelecter(String[][] board, String prize, Random rand) {
...

or alternatively change what you're returning

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