所以我试图让这个方法接受两个 int 数组,如果第一个数组中的每个元素都小于第二个数组中相同索引处的元素,则返回 true 如果数组的长度不同,那么它将返回与较短数组的长度进行比较。这是我到目前为止所拥有的,但我一直在两个 j 单元测试中失败,并且无法弄清楚是什么原因造成的。感谢您提前提供的任何帮助。
这是我失败的两个 junit 测试
@Test
public void testSecondLessFirstLonger() {
int[] one = { 5, 5, 5 };
int[] two = { 4 };
boolean actual = Program1.allLess( one, two );
assertFalse( "Incorrect result",actual );
}
@Test
public void testSecondLessSecondLonger() {
int[] one = { 2 };
int[] two = { 1, 0 };
boolean actual = Program1.allLess( one, two );
assertFalse( "Incorrect result",actual );
}
import java.util.Arrays;
这是我到目前为止的代码
public class Program1 {
public static void main(String[] args)
{
int[] one = { 2 };
int[] two = { 1, 0 };
System.out.println(allLess(one, two));
}
public static boolean allLess(int[] one,int[] two)
{
if (one.length != two.length)
{
int len = 0;
if(one.length <= two.length)
{
len = one.length;
}
if(two.length < one.length)
{
len = two.length;
}
boolean[] boo = new boolean[len];
for(int i = 0; i < len; i++)
{
if(one[i] < two[i])
{
boo[i] = true;
}
else
{
boo[i] = false;
}
}
if(Arrays.asList(boo).contains(false))
{
return false;
}
else
{
return true;
}
}
for (int i = 0; i < one.length; i++)
{
if (one[i] >= two[i])
{
return false;
}
}
return true;
}
}
最佳答案
也许你可以尝试这样的事情:
public static boolean allLess(final int[] array1, final int[] array2){
for(int i = 0; i < Math.min(array1.length, array2.length); i++)
if(array1[i] >= array2[i])
return false;
return true;
}
关于java - boolean allLess(int[]一,int[]二)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18677395/