package sort;
import java.util.Arrays;
import java.util.Scanner;
public class TwoSum {
public static void main(String[] args) {
int[]arr= {1,3,5,7,9,11};
int target=new Scanner(System.in).nextInt();
int []arr2=calulate(arr, target);
while (arr2 == null){
System.out.println("数组中没有可以合成该target的两个数!请输入其他target:");
target = new Scanner(System.in).nextInt();
arr2=calulate(arr,target);
}
System.out.println(Arrays.toString(arr2));
}
public static int[]calulate(int[]arr,int target){
int []nums=null;
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i]+arr[j]==target) {
nums=new int[] {i,j};
}
}
}
return nums;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)