Java 我的";“用于循环”;韩元';t实际上是循环,但它';It’他不会抛出任何代码,我也不会';我看不出有什么问题
所有这些只是供参考,这样你可以看到发生了什么,如果你需要看。我正在用必需的字段和方法创建一个网格游戏。为了便于使用,我在作业中添加了一些: char[][]棋盘 boxTop() boxBottom() printws()Java 我的";“用于循环”;韩元';t实际上是循环,但它';It’他不会抛出任何代码,我也不会';我看不出有什么问题,java,loops,Java,Loops,所有这些只是供参考,这样你可以看到发生了什么,如果你需要看。我正在用必需的字段和方法创建一个网格游戏。为了便于使用,我在作业中添加了一些: char[][]棋盘 boxTop() boxBottom() printws() import java.util.Scanner; 导入java.util.Random; 公共类清扫器名称{ private char[][]gameBoard;//表示网格。[行][列] //对于访问,需要两个嵌套的For循环,它们将进行排序、添加和跟踪 私有字符[][]
import java.util.Scanner;
导入java.util.Random;
公共类清扫器名称{
private char[][]gameBoard;//表示网格。[行][列]
//对于访问,需要两个嵌套的For循环,它们将进行排序、添加和跟踪
私有字符[][]棋盘;
私营企业;
私人财务;
私人行动;
发现私有布尔值;
公共静态void main(字符串[]args){//驱动程序文件
扫描仪s=新的扫描仪(System.in);
内部高度;
整数宽度;
布尔完成=假;
int x;
int-y;
做{
System.out.println(“扫沙寻宝”);
System.out.println(“请输入高度和宽度的编号”
+“\n游戏板的名称”);
系统输出打印(“板的高度:”);
高度=s.nextInt();
系统输出打印(“电路板宽度:”);
宽度=s.nextInt();
SweeperGame游戏=新的SweeperGame(高度、宽度);
s、 nextLine();
做{
做{
System.out.print(“输入由空格分隔的宝藏坐标:”);
x=s.nextInt();
y=s.nextInt();
}而(!game.checkOutOfBounds(x,y)| | game.beenSwept(x,y));
完成=game.digSand(x,y);
System.out.println(game.toString());
s、 nextLine();
}而(!完成);
System.out.println(“你在“+game.getTotalMoves()+”moves!”中找到了宝藏);
System.out.println(“out of”+game.getBoardHeight()*game.getBoardWidth()+“可能的移动”);
System.out.print(“您是否希望找到更多宝藏[Y/N]?”;
}while(s.nextLine().equalsIgnoreCase(“Y”);
}
公共扫掠器名称(整数高度、整数宽度){
/*
接收两个int参数,作为用户对埋藏宝藏的网格的高度和宽度的输入
实例化游戏板阵列[x][y]
初始化游戏板单元格以包含空格“”。仅使用单引号完成。(为什么单引号很重要?)
分别使用第一个/第二个参数随机设置司库X/司库Y
将gameBoard[TreaseX][TreaseY]设置为“T”
将总移动次数设置为0
设置为false
*/
totalMoves=0;
发现=错误;
//构建网格
如果(高度==0和宽度==0){
抛出新的IllegalArgumentException();
}
//实例化游戏板
gameBoard=新字符[高度][宽度];
checkGameBoard=gameBoard;//可能必须将其移动到设置后的宝藏中。我们将看到它的效果
//初始化游戏板的单元格
对于(int i=0;i
这看起来可疑
编辑:使用以下命令:
import java.util.ArrayList;
import java.util.List;
public class Spawner {
public static void main(String[] args) {
/* Declaration/Construction/Initialization of listOfLists begins here. */
List<List<Integer>> listOfLists = new ArrayList<List<Integer>>(0);
for(int rows = 0; rows < 10; rows++) {
listOfLists.add(new ArrayList<Integer>(0));
for(int columns = 0; columns < 10; columns++) {
listOfLists.get(rows).add(Integer.valueOf(rows * columns));
}
}
/* Declaration/Construction/Initialization of listOfLists ends here. */
/* What's the advantage of this construct? It is dynamic,
* you are able to change its size while runtime. */
for(int rows = 0; rows < listOfLists.size(); rows++) {
for(int columns = 0; columns < listOfLists.get(rows).size(); columns++) {
System.out.print(listOfLists.get(rows).get(columns).intValue() + " ");
}
System.out.println("\n");
}
}
}
import java.util.ArrayList;
导入java.util.List;
公共类产卵器{
公共静态void main(字符串[]args){
/*列表的声明/构造/初始化从这里开始*/
列表列表=新的ArrayList(0);
对于(int行=0;行<10;行++){
添加(新的ArrayList(0));
for(int columns=0;columns<10;columns++){
get(rows).add(Integer.valueOf(rows*columns));
}
}
/*ListofList的声明/构造/初始化到此结束*/
/*这种结构的优点是什么?它是动态的,
*您可以在运行时更改其大小。*/
对于(int rows=0;rows
我没有办法得到每个维度的长度,因为它们是可变的。数组不能是动态的,它们必须是静态的,也就是说,你需要知道它们的维度。
public String toString(){ // Displays the current gameBoard array and it's contents except for the location of the treasure
int rows = gameBoard.length;
int columns = gameBoard[1].length;
for (int i = 0; i <= rows; i++) {
boxTop();
printSubRows(); // Prints top and the sub-rows
for (int j = 0; j < columns; j++) { // Columns of gameBoard
return "| " + checkGameBoard[i][j] + " ";
}
printSubRows();
return null;
}
boxBottom();
return null;
/*
Prints the following:
| | | | |
| | | | |
| | | | |
| x | x | x | x |
| | | | |
| | | | |
| | | | |
*/
}
public String boxTop(){
return ("--------".repeat(gameBoard.length)+"\n");
}
public String boxBottom(){
return "--------".repeat(gameBoard.length);
}
public String printSubRows(){
String filler = ("| ").repeat(gameBoard[1].length) + "\n";
return (filler.repeat(3)); // 3 is the number of sub-rows before a character input is required for the grid.
}
}
int rows = gameBoard.length;
int columns = gameBoard[1].length;
import java.util.ArrayList;
import java.util.List;
public class Spawner {
public static void main(String[] args) {
/* Declaration/Construction/Initialization of listOfLists begins here. */
List<List<Integer>> listOfLists = new ArrayList<List<Integer>>(0);
for(int rows = 0; rows < 10; rows++) {
listOfLists.add(new ArrayList<Integer>(0));
for(int columns = 0; columns < 10; columns++) {
listOfLists.get(rows).add(Integer.valueOf(rows * columns));
}
}
/* Declaration/Construction/Initialization of listOfLists ends here. */
/* What's the advantage of this construct? It is dynamic,
* you are able to change its size while runtime. */
for(int rows = 0; rows < listOfLists.size(); rows++) {
for(int columns = 0; columns < listOfLists.get(rows).size(); columns++) {
System.out.print(listOfLists.get(rows).get(columns).intValue() + " ");
}
System.out.println("\n");
}
}
}