Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions