问题描述
首选结果:向用户询问板的宽度和长度。使用给定的维度创建网格。在打印网格之前,将随机元素从“ ”替换为“F”。
当前结果:成功地向用户询问宽度和长度但不替换元素。而是打印空白网格。
我在下面尝试过的:我使用 ListIterator 遍历列表并检查是否有任何元素处于隐藏状态(空白),如果是,则将选择 1-10 中的随机数,如果该数字低于5,元素将被替换,允许50%的棋盘是炸弹
如何修复此代码,以便在应用时将空白框替换为“F”?
import java.util.*;
public class GameBoard {
List<String> rows;
List<String> rowSeparator;
String hiddenState = " ";
String foundState = "F";
Scanner scanner;
Random randomBox;
public static void main(String[] args){
GameBoard main = new GameBoard();
main.generateGrid();
main.printGrid();
}
GameBoard(){
scanner = new Scanner(system.in);
randomBox = new Random();
}
String getState(){
return this.state;
}
void setState(String state){
this.state = state;
}
void generateGrid() {
int width;
rows = new ArrayList<>(20);
rowSeparator = new ArrayList<>();
System.out.print("Width?: ");
width = scanner.nextInt();
for (int i = 0; i < width; i++) {
rows.add("|");
setState(hiddenState);
rows.add(getState());
rowSeparator.add("-");
rowSeparator.add("-");
int number = randomBox.nextInt(11);
ListIterator<String> rowListIterator = rows.listIterator();
while (rowListIterator.hasNext()) {
String bombLocation = rowListIterator.next();
if (bombLocation.equals(hiddenState)) {
if (number < 5) {
bombLocation.replace(hiddenState,foundState);
}
}
}
}
}
void printGrid(){
int length;
System.out.print("Length?: ");
length = scanner.nextInt();
for(int i = 0; i < length; i++){
System.out.println(rows.toString().replace("[","").replace("]","").replace(",","") + " |");
System.out.println(rowSeparator.toString().replace("[","") + " -");
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)