如何编写测试打印结果? 【JAVA]

时间:2022-11-29 21:37:02

How can I write a test to print this result?

如何编写测试来打印此结果?

package leetcode_one_twenty;

import java.util.HashMap; // HashMap package

public class Two_Sum {

    public int[] twoSum(int[] numbers, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < numbers.length; i++) {
            if (map.get(numbers[i]) != null) {
                int[] result = {map.get(numbers[i]) + 1, i + 1};
                return result;
            }
            map.put(target - numbers[i], i);
        }

        int[] result = {};
        return result;
    }

    public static void main(String[] args) {

        // How can I write a test to print this result? THX!
    }

}

2 个解决方案

#1


1  

Make your twoSum method static, and call it with values from your main method:

使您的twoSum方法保持静态,并使用main方法中的值调用它:

int[] myArray = {1,2,3};
int target = 5;
System.out.println(Arrays.toString(twoSum(myArray, target)));

#2


0  

I'm not really sure what are you exactly trying to do. Why not try System.out.println() and toString()?

我不确定你到底想要做什么。为什么不尝试System.out.println()和toString()?

#1


1  

Make your twoSum method static, and call it with values from your main method:

使您的twoSum方法保持静态,并使用main方法中的值调用它:

int[] myArray = {1,2,3};
int target = 5;
System.out.println(Arrays.toString(twoSum(myArray, target)));

#2


0  

I'm not really sure what are you exactly trying to do. Why not try System.out.println() and toString()?

我不确定你到底想要做什么。为什么不尝试System.out.println()和toString()?