gpt4 book ai didi

Java 二维数组,测试正方形

转载 作者:行者123 更新时间:2023-11-29 07:15:21 25 4
gpt4 key购买 nike

我得到了一个数组 (a2d),我需要确定每一行和每一列的元素数量是否与其他每一行和每一列的元素数量相同。如果是,那么我将 boolean 值 isSquare 设置为 true。

我想出了以下代码,但它不喜欢它,也没有给我任何关于如何改进它的建议。

for(int row = 0; row < a2d.length; row++){
for(int col = 0; col < a2d[row].length; col++)
if(a2d.length == a2d[row].length)
isSquare = true;
else
isSquare = false;
}

我的测试方法是错误的还是有更好的方法?

最佳答案

不需要 2 个循环你应该能够做这样的事情(我不打算给出代码,因为它是家庭作业)

1. Save the length of the array (a2d.length)
2. Loop over all the rows
3. Check to see if the given row has the same length
4. if Not return false
5. if you reach the end of the loop return true

关于Java 二维数组,测试正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10021599/

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