#java #arrays
#java #массивы
Вопрос:
Я пишу программу penny pitch на Java, и это карнавальная игра. Призы генерируются случайным образом на доске, и бросается 10 монет. В методе «randSelector», который я написал, я получаю сообщение об ошибке от «prize», потому что в нем говорится, что я не могу вернуть строку в String []. Есть предложения? Это мой код:
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] != "[ ]" amp;amp; board[pick 1][anotherNum 1] == "[ ]" amp;amp; pick != 24) {
board[pick 1] = prize;
} else if (board[pick][anotherNum] != "[ ]" amp;amp; pick != 0 amp;amp; 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];
}
}
}
}
Ответ №1:
Ваш возвращаемый тип в randSelecter
is String[]
, но вы возвращаете a String
, выполняя return board[x][p]
. Измените возвращаемый тип на String
:
public static String randSelecter(String[][] board, String prize, Random rand) {
...
или, в качестве альтернативы, измените то, что вы возвращаете
Комментарии:
1. ОК. Та же ошибка возникает с частью инструкции prize not return. Приз говорит, что я не могу преобразовать String в String[] . Любые другие предложения?
2. Извините, нам нужно больше деталей, чтобы помочь вам. Пожалуйста, предоставьте трассировку