In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
import java.util.Scanner;
public class JavaLoopExcercise
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int number;
char choice;
int even = 0;
int odd = 0;
do
{
System.out.print("Enter any number ");
number = console.nextInt();
if( number % 2 == 0)
{
even += number;
}
else
{
odd += number;
}
System.out.print("Do you want to continue y/n? ");
choice = console.next().charAt(0);
}while(choice=='y' || choice == 'Y');
System.out.println("Sum of even numbers: " + even);
System.out.println("Sum of odd numbers: " + odd);
}
}
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
Result:
Enter any number 5
Do you want to continue y/n? y
Enter any number 4
Do you want to continue y/n? y
Enter any number 6
Do you want to continue y/n? y
Enter any number 3
Do you want to continue y/n? n
Sum of even numbers: 10
Sum of odd numbers: 8
need an explanation for this answer? contact us directly to get an explanation for this answer