Question: Need help in java: Write a program that prompts for a sequence of integer inputs and displays cumulative totals. For example, if the input is

Need help in java: Write a program that prompts for a sequence of integer inputs and displays cumulative totals. For example, if the input is 1 7 2 9, the program should print 1 8 10 19.

I have my code just about almost finished but it is not calculating the totals correctly.

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

int num;

int sum=0;

System.out.println("Enter four integers: ");

num = in.nextInt();

//while there is input

while(in.hasNext()){

//add input to sum

sum=sum+in.nextInt();

//print the sum

System.out.print(sum+" ");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!