Hello everyone.
I've a bit of trouble with my code. I'm suppose to create a program that will allow the user to input as many integers as they desire. When the user is done, the program will calculate the alternating sum. The program will then display the equation along with the answer to the user. The program also has to recognize "0" as a symbol meaning the user is done inputting integers. For example, if the user were to input 1 4 9 16 9 7 4 9 11 0, the program will display 1-4+9-16+9-7+4-9+11=-2. So far, I've gotten everything but displaying the equation. Please help me; I've been at this for several days now.
This is what I currently have for my code:
(And thank you to everyone who helps.)
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
int num;
int total=0;
int i=0;
Scanner sc = new Scanner(System.in);
do
{
System.out.print("Input an integer. Input 0 to finalize: ");
num = sc.nextInt();
if (i==0)
{
total = num;
}
else
{
if (i%2==0)
{
total = total + num;
}
else
{
total = total-num;
}
}
i++;
}
while (num!=0);
System.out.println(i);
System.out.println(total);
}
}