How can I skip numbers in a for loop using only 1 loop and 2 if statement? This is the sample picture
public class Main {
public static void main(String[] args) {
int x = 0, y = 0, sum = 0;
for (y = 1; y <= 99; y ) {
x = x 1;
y = y 1;
if (x <= 10) {
sum = sum y;
}
if (x == 20) {
x = 0;
}
}
System.out.println(sum);
}
I want to output 2275. I want to add 1 to 10, 21 to 30, 41 to 50, 61 to 70, 81 to 90 together I just follow the flowchart but I think there is a problem
CodePudding user response:
It seems like continue
is not required here. Since it asks to use for-loop, the Initialization and increment/decrement part can be left empty.(while-loop suits more in this scenario).
int x = 0, y = 0, sum = 0;
for (; y <= 99;) {
x = x 1;
y = y 1;
if (x <= 10) {
sum = sum y;
}
else if (x==20)
{
x = 0;
}
}
//print sum using joption
CodePudding user response:
I think you flowchart may be translated like this:
package com.company;
public class Main {
public static void main(String[] args) {
int x = 0, sum = 0;
for (int y = 0; y <= 99; y ) {
x ;
if (x <= 10) {
sum = y;
} else if (x == 20) {
x = 0;
}
}
System.out.println(sum);
}
}