+ 2
Array and they sum
public class Program { public static void main(String[] args) { int[ ] primes = {2, 3, 5, 7}; int sum = 0; for (int x=0;x<primes.length;++x) { sum+=primes[x]; System.out.println(sum); } } } why output is //2 5 10 17?? it should be // 17 ! what I do a wrong? heeelp!)
5 odpowiedzi
+ 18
//Array and they sum
/*print statement must be out of for loop , if its inside the loop then sum will be printed in each cycle of the loop*/
public class Program {
public static void main(String[] args) {
int[ ] primes = {2, 3, 5, 7};
int sum = 0;
for (int x=0;x<primes.length;++x) {
sum+=primes[x];
}System.out.println(sum);
}
}
+ 8
That's why!
public class Program {
public static void main(String[] args) {
int[ ] primes = {2, 3, 5, 7};
int sum = 0;
for (int x=0;x<primes.length;++x) {
sum+=primes[x];
System.out.println(sum); // print to console executes in each loop cycle [2 5 10 17]
}
}
}
public class Program {
public static void main(String[] args) {
int[ ] primes = {2, 3, 5, 7};
int sum = 0;
for (int x=0;x<primes.length;++x) {
sum+=primes[x];
}
System.out.println(sum); // print to console executes once after loop finished its work [17]
}
}
+ 8
@Sensation
Try to send your Q in a separate post.
+ 2
Gaurav and Babak Thank you guys! Your explanation is help me!
+ 1
ok