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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started