簡體   English   中英

boolean allLess(int [] one,int [] two)方法

[英]boolean allLess(int[] one,int[] two) method

因此,我試圖使這種方法接受兩個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;
}

//這種方式也可以。 程序3完成了嗎? 它給我問題

import java.lang.Math.*;

public class Program1 {
public static boolean allLess(int[] one, int[] two) {

    if (one == null || two == null) {
        return false;
    }

    for (int i = 0; i < Math.min(one.length, two.length); i++) {

        if (two[i] <= one[i]) {
            return false;
        }
    }
    return true;
}

public static void main(String[] args) {

    System.out.println(" ");
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM