Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何验证数组是否包含数据,如果是>;can';不要覆盖它_Java_Arrays - Fatal编程技术网

Java 如何验证数组是否包含数据,如果是>;can';不要覆盖它

Java 如何验证数组是否包含数据,如果是>;can';不要覆盖它,java,arrays,Java,Arrays,我正在为用户和计算机之间的战舰游戏编写一个代码,首先我要求用户位置放置他们的战舰和手榴弹(每个需要1个位置),不能在同一位置放置2艘战舰或2枚手榴弹 如何编写代码来验证阵列位置是否已被“占用”,以及是否请求用户重试 我试着写:但显然我写不出来!无效的所以,我不知道该写什么 public void gridverification(String[][] grid){ for(int i =0; i<grid.length; i++){ for(int j = 0; j<gri

我正在为用户和计算机之间的战舰游戏编写一个代码,首先我要求用户位置放置他们的战舰和手榴弹(每个需要1个位置),不能在同一位置放置2艘战舰或2枚手榴弹

如何编写代码来验证阵列位置是否已被“占用”,以及是否请求用户重试

我试着写:但显然我写不出来!无效的所以,我不知道该写什么

public void gridverification(String[][] grid){

for(int i =0; i<grid.length; i++){
    for(int j = 0; j<grid.length; i++){

        if(grid[i][j] == !null){

        }
    }
}
下面是我做的验证方法:

公共void网格验证(字符串[][]网格){

if(grid!=null&&grid.length>0)
{
对于(int i=0;i
您的逻辑有问题。将其写成
if(grid[i][j]!=null)
。这样您就可以看到该位置的网格是否不等于null。

您应该检查给定的网格数组和[i][j]处的实际元素。您还可以像
((grid[i][j]!=null)&(grid[i][j]那样检查元素的大小.length()>0))

公共void网格验证(字符串[][]网格){
if(grid!=null&&grid.length>0){
对于(int i=0;i
或者,如果您正在处理一个大型项目,您经常需要检查字符串变量的null及其大小,那么您应该使用
StringUtils.isEmpty
StringUtils.isNotBlank
,方法是在项目中添加
commons-lang-2-6.jar
作为依赖项jar


您似乎正在尝试检查用户是否已经在他们输入的位置放置了一艘飞船。您当前的代码没有这样做。它会检查整个数组,检查每个位置是否为空。如果他们已经输入了一艘飞船,它会找到它并说他们需要重试。您可以做的只是检查他们所输入的位置输入。因此,您可以执行类似以下操作:

public String[][] populateWithShips(String[][] grid){
    int shipCount = 6;
    for (int shipIndex = 0; shipIndex < shipCount; shipIndex++) {
        boolean shipExists = false;
        while(shipExists != true){//Another way you could do this is (!shipExists)
            System.out.println("Please enter the column number for ship number " + shipIndex + :");
            int column = Integer.parseInt(this.keyboard.next());//This will take the input String and parse it to a Integer object
            System.out.println("Please enter the row number for ship number " + shipIndex + :");
            int row = Integer.parseInt(this.keyboard.next());
            //Note I haven't verified that this inputs are correct, just assuming they user enter good inputs

            if(grid[column][row] != null){
                System.out.println("Sorry, coordinates already used. Try again.\n");
            }
            else{
                grid[column][row] = " s ";
                shipExists = true;
            }
        }   
    }
    return grid;
}
public String[]]populateWithShips(String[]]grid){
国际船舶计数=6;
对于(int-shipIndex=0;shipIndex
请注意,
grid[row][column]
更准确,但只要您保持一致,就无所谓了


此外,这似乎是家庭作业的一部分,因此我建议您仔细阅读。我还建议您使用调试器并写出您试图实现的步骤。这将帮助您更清楚地了解您正在做什么,并更容易发现错误。

if(grid[I][j]!=null){
internal
对于
,它不是
i++
,而是
j++
而不是网格数组的数据类型是什么?谢谢…哇,这么简单的语法问题…这是一个字符串数据您输入了一个错误“!==”我尝试过,如果该条件为真,我希望它向用户显示“对不起,已经使用的坐标,再试一次”但是,即使是一个新值,它也会不断显示消息…我已经用代码更新了我的初始问题。您应该在另一个数组中捕获初始数组,该数组可用于与新数组中的新值进行比较,如果它们相同,则提醒用户,否则继续处理,因为它们是新值。谢谢,我感谢您他帮了我的忙-也谢谢你发了这封公开信,我也不知道在哪里可以找到这样的调试器?我正在用Intelij IDEA写代码
if (grid != null && grid.length > 0)
{
    for (int i = 0; i < grid.length; i++)
    {
        for (int j = 0; j < grid.length; j++)
        {

            if (grid[i][j] != null)
            {
                System.out.println("Sorry, coordinates already used. Try again.");
            }
        }
    }
}
public void gridverification(String[][] grid) {
    if (grid != null && grid.length > 0) {
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid.length; i++) {
                if (grid[i][j] != null) {
                    // do something
                }
            }
        }
    }
}
public String[][] populateWithShips(String[][] grid){
    int shipCount = 6;
    for (int shipIndex = 0; shipIndex < shipCount; shipIndex++) {
        boolean shipExists = false;
        while(shipExists != true){//Another way you could do this is (!shipExists)
            System.out.println("Please enter the column number for ship number " + shipIndex + :");
            int column = Integer.parseInt(this.keyboard.next());//This will take the input String and parse it to a Integer object
            System.out.println("Please enter the row number for ship number " + shipIndex + :");
            int row = Integer.parseInt(this.keyboard.next());
            //Note I haven't verified that this inputs are correct, just assuming they user enter good inputs

            if(grid[column][row] != null){
                System.out.println("Sorry, coordinates already used. Try again.\n");
            }
            else{
                grid[column][row] = " s ";
                shipExists = true;
            }
        }   
    }
    return grid;
}