gpt4 book ai didi

java - 如何检查二维数组中的值是否为某个数字?

转载 作者:行者123 更新时间:2023-11-29 05:03:14 27 4
gpt4 key购买 nike

<分区>

大家好,我正在研究 ulam 的螺旋问题,我想搜索我创建的二维数组并找到用户输入的数字 (numb)。我不明白为什么 if 语句(标有注释)总是返回 false

编辑:这是部分代码,我已经通过调试器运行了我的 genUlam 函数,它实际上返回了数字,不幸的是这是一个任务,所以我无法发布整个代码。

public static void main(String[] args)
{
//create scanner object
Scanner input = new Scanner(System.in);

//get input for dimensions
//System.out.print("Enter dimension: ");
int dimension = 3;

//check if dimensions are odd
if (isOdd(dimension) == false)
{
//if input for dimensions is even, get the next odd dimension
dimension += 1;
}

//get input for number in center
// System.out.println("Enter number in spiral: ");
int numb = 2;


//check that number in center is in range of 1 to dim^2
if (numb > Math.pow(dimension,2) && numb < 1)
{
System.out.println("Number not in range");
System.exit(0);
}

//create 2D array using my function
String[][] ulam = {{"1", "2", "3"},{"4","5","6"},{"7","8","9"}};

//check to see if the number user entered does exist, then print array
for(int r = 0; r < dimension - 1; r++)
{
for(int c = 0; c < dimension - 1; c++)
{
//String current = ulam[r][c];
/*the line below should be returning true when match is found, but it is returning false no matter what*/
if(ulam[r][c].equals(Integer.toString(numb)))
{
printArray(ulam, r, dimension);
}
else

{

System.out.println("match not found");
}
}
}

}

public static void printArray(String[][] array, int row, int dimension)
{
for (int i = 0; i < dimension - 1; i++)
{
System.out.println(array[row][i] + " ");
}
}


}

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