Question: 1) can u see my code, why it has nothing print out public class Array { public static void productXY(int[] a) { int sum =

1) can u see my code, why it has nothing print out

public class Array {

public static void productXY(int[] a) {

int sum = 0;

int x = 0;

int y = 0;

for (int s = 0; s < a.length; s++) {

sum = sum + a[s];

// array's total sum.

}

for (int i = 0; i < a.length; i++)

for (int j = 0; j < a.length; j++) {

if (i < j) {

if (a[i] * a[j] == sum) {

// test if there are 2 product x*y = sum.

x = a[i];

y = a[j];

if(a[i] * a[j] == sum) {

System.out.println("Two elements with product equal to array sum: " + x + " and " + y);

} else {

System.out.println("Two elements with product equal to array sum: " + "no");

}

}

}

}

if (a.length <= 1)//if there don't have 2 product

System.out.println("Two elements with product equal to array sum: " + "no");

}

public static void main(String args[]) {

int b[]= {-2, 59, 23, -76, -45, -81, -74, 70, -16, -95, 67, 77, 8, -88, -35, 47, -17, 0, 19};

//-299, no

productXY(b);

}

}

-------------------------

2) if i do like this, if will have too many print out

public class Array {

public static void productXY(int[] a) {

int sum = 0;

int x = 0;

int y = 0;

for (int s = 0; s < a.length; s++) {

sum = sum + a[s];

// array's total sum.

}

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a.length; j++) {

if (i < j) {

if (a[i] * a[j] == sum) {

// test if there are 2 product x*y = sum.

x = a[i];

y = a[j];

}

if (a[i] * a[j] == sum) {

System.out.println("Two elements with product equal to array sum: " + x + " and " + y);

} else {

System.out.println("Two elements with product equal to array sum: " + "no");

}

}

}

}

if (a.length <= 1)// if there don't have 2 product

System.out.println("Two elements with product equal to array sum: " + "no");

}

public static void main(String args[]) {

int b[] = { -2, 59, 23, -76, -45, -81, -74, 70, -16, -95, 67, 77, 8, -88, -35, 47, -17, 0, 19 };

// -299, no

productXY(b);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!