编程语言
首页 > 编程语言> > java -练习题-从给定数组中选出和为固定值的两个数

java -练习题-从给定数组中选出和为固定值的两个数

作者:互联网

package com.etc;


/**
 * 
 *  从给定数组中选出和为固定值的两个数
 * 
 */
public class Test {
    public static void main(String[] args) {
        int[] nums = {1, 5, 9, 11, 3, 33, 11, 45, 67};
        Test test = new Test();
        test.sum(nums, 14);
    }

    public int[] sum(int[] nums, int target) {
        int data[] = new int[2];
        for (int i = 0; i < nums.length; i++) {

            for (int j = i + 1; j < nums.length-1; j++) {
                    if (nums[i] + nums[j]  == target) {
                        data[0] = i;
                        data[1] = j;
                    System.out.println(i + "," + j );
                    }
                }
            }
        return data;
    }
}

标签:练习题,固定值,java,nums,int,Test,data,public
来源: https://blog.csdn.net/qq_43617838/article/details/115269724