【发布时间】:2017-07-05 20:35:01
【问题描述】:
我正在构建一个用户定义的数组作为游戏板。字符使用“O”和“。”必须是随机的,并且“O”必须出现不止一次。
这是我迄今为止所拥有的。
import java.util.Scanner;
public class PacMan {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Input total rows:");
int row = input.nextInt();
System.out.println("Input total columns:");
int column = input.nextInt();
boolean[][] cookies = new boolean[row+2][column+2];
for (int i = 1; i <= row; i++)
for (int j = 1; j <= column; j++);
cookies [row][column] = (Math.random() < 100);
// print game
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= column; j++)
if (cookies[i][j]) System.out.print(" O ");
else System.out.print(". ");
System.out.println();
}
}
}
例如,输出会产生一个 5 x 5 的网格,但“O”只出现一次并且位于网格的右下角。
辅助随机化“O”和“.”并让“O”以随机方式出现在整个板上,由用户通过扫描仪输入初始化。
这是生成我正在寻找的输出并且是用户定义的更新代码。
import java.util.*;
public class PacManTest
{
public static void main(String[] args)
{
char O;
Scanner input = new Scanner(System.in);
System.out.println("Input total rows:");
int row = input.nextInt();
System.out.println("Input total columns:");
int column = input.nextInt();
char board[][] = new char[row][column];
for(int x = 0; x < board.length; x++)
{
for(int i = 0; i < board.length; i++)
{
double random = Math.random();
if(random >.01 && random <=.10)
{
board[x][i] = 'O';
}
else {
board[x][i] = '.';
}
System.out.print(board[x][i] + " ");
}
System.out.println("");
}
}
}
【问题讨论】: