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);
}
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

String sum = "";
if ( sum.equals(""))
sum += num;
else
sum += " + " + num;
or sum += " - " + num;
and within each if where you add or subtract
depending on where you are in your code

all you need to do is to store the numbers in another class

help.java

import java.util.Scanner;
import java.util.*;

public class help
{

public static void main(String[] args)
{
int num;
int total=0;
int i=0;

help2[] entries=new help2[100];
int counter=0;

Scanner sc = new Scanner(System.in);

do
{
System.out.print("Input an integer. Input 0 to finalize: ");
num = sc.nextInt();

   entries[counter]=new help2(num);
   counter++;

if (i==0)
{
total = num;
}
else
{
if (i%2==0)
{
total = total + num;
}
else
{
total = total-num;
}
}
i++;
}
while (num!=0);
System.out.println("The number of input is:" +i);
   for(int j=0;j<counter-1;j++){
	if (j%2==0)
	{
		if(j!=counter-2){
			System.out.print(entries[j].getNum()+ "-");
		}	
		else{	
			System.out.print(entries[j].getNum());
		}
	}else{
		if(j!=counter-2){
			System.out.print(entries[j].getNum()+ "+");
		}	
		else{	
			System.out.print(entries[j].getNum());
		}
	}
}
System.out.println("="+total);
}
}

help2.java

public class help2{

private int num;

   public help2(int num){
	this.num = num;
   }
   public void setNum(int num){
   this.num=num; 
   }
   public int getNum(){
   return this.num;
   }
}
Comments
Don't post answers to questions. It's not YOUR homework.

@zeroliken just a remark ... just to print it that way, having it stored in a String object as I've shown, just adding about 4 lines of code would be sufficient, agreed, not a very dynamical way, but would do the trick here.

if you do want to put them all in an array, why bother creating a new class? why not just using an array of int's?

@stultuske you have a good point and I admit that my way is far from the best solution its just that I've gotten used to using a new class in my works.

anyways it was only a suggestion :)

This question has already been answered. Start a new discussion instead.