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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started