有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何启用系统。出来在eclipse中运行Junit测试时使用println()?

我的java代码如下:

package ex2_binarySearch;

public class Solution {
//  public int [] twoSum(int target, int [] numbers) {
    public int [] twoSum(int [] numbers, int target) {
        for(int i = 0; i < numbers.length; i++) {
            int res = binarySearch(i, numbers.length, target - numbers[i], numbers);
            if(res != -1) {
                System.out.println("Yeah");
                return new int [] {i+1, res+1};
            }
        }
                System.out.println("Yeah Yeah");
        throw new IllegalArgumentException("Not found");
    }

    public int binarySearch(int start, int end, int target, int [] numbers) {
        int L = start;
        int R = end;
        while (L < R) {
            int M = (L+R)/2;
            if(numbers[M] < target) {
                L = M+1;
            }
            else if(numbers[M] > target) {
                R = M-1;
            }
            else {
                return M;
            }
        }
        return -1;
    }
}

以下是Junit测试:

package ex2_binarySearch;

import static org.junit.Assert.*;

import org.junit.Test;

import ex1.Solution;

public class SolutionTest {

    @Test
    public void test() {
    /*
        Solution s = new Solution();
        int [] numbers = {1, 2, 3, 4};
        int target = 4;
        int [] res = s.twoSum(numbers, target);
        assertArrayEquals(new int[] {1, 3}, res);
        */
        Solution s = new Solution();
        int [] numbers = {-3,3,4,90};
        int target = 0;
        int [] res = s.twoSum(numbers, target);
        assertArrayEquals(new int[] {1, 2}, res);
    }

}

当我执行Junit测试时,eclipse控制台中没有输出任何内容。这是为什么?我如何启用它?我注意到,如果我用main函数编写一个程序,并且只运行main而不是junit,那么打印结果将到达控制台


共 (0) 个答案