gpt4 book ai didi

java - 不进入for循环内部

转载 作者:行者123 更新时间:2023-12-02 01:30:12 24 4
gpt4 key购买 nike

我正在尝试为电话拨号盘创建一个号码关联映射,如下所示:

private Integer[][] dialpad = { {1   , 2   , 3},
{4 , 5 , 6},
{7 , 8 , 9},
{null, 0 , null} };

现在,根据规则关联为:

1 should 2,4,5
2 should be 1,3,4,5,6
3 should be 2,5,6
and so on............

我已经编写了代码来处理这个问题:

public void createDialAssociation()
{
int nosCols = dialpad[0].length;
int nosRows = dialpad.length;
//int rowFrom, rowTo, colFrom, colTo;

for(int row=0; row<nosRows; row++)
{
for(int col=0; col<nosCols; col++)
{
Integer currentElement = dialpad[row][col];
elementAssociation.put( currentElement, new ArrayList<Integer>());

int rowFrom = (row-1)< 0 ? 0 : (row-1);
int rowTo = (row+1) <= (nosRows-1)? row+1: nosRows-1;
int colFrom = (col-1)<0 ? 0 : (col-1);
int colTo = (col+1) <= (nosCols-1) ? col+1 : nosCols-1;

LOG.info("row,col,element = " + row + "," +col+ ","+ currentElement);
LOG.info("rowFrom = " + rowFrom);
LOG.info("rowTo = " + rowTo);
LOG.info("colFrom = " + colFrom);
LOG.info("colTo = " + colTo);
LOG.info("---------------------------------------------------- " );
for(int currentRowIndex=rowFrom; currentRowIndex==rowTo; currentRowIndex++)
{
LOG.info("1..............");
for(int currentColIndex = colFrom; currentColIndex == colTo ; currentColIndex++)
{
LOG.info("2..............");
if ( currentRowIndex == row || currentColIndex == col )
continue;

if( dialpad[currentRowIndex][currentColIndex] != null)
{
elementAssociation.get(currentElement).add(dialpad[currentRowIndex][currentColIndex]);
}

}
}

}
}

我的问题是,代码没有进入循环:

for(int currentRowIndex=rowFrom; currentRowIndex==rowTo; currentRowIndex++)

因此,我没有看到

LOG.info("1.......);
or LOG.info("2........);

我可以期待任何见解/帮助吗?

最佳答案

for(int currentRowIndex=rowFrom; currentRowIndex==rowTo; currentRowIndex++){}

初始化currentRowIndex作为 rowFrom 的值。而currentRowIndex==rowTo它运行循环的内容,并在每次执行循环后运行 currentRowIndex++ 。因此,循环最多在 rowFrom==rowTo 时进入一次。 .

您可能想继续循环直到 currentRowIndex==rowTo ,甚至可能包括那个案例。这意味着您必须运行 continue the 循环 while rowFrom仍低于rowTo ,所以你必须写 rowFrom<rowTorowFrom<=rowTo .

内循环也是如此。

关于java - 不进入for循环内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56208068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com