How i can fix the increasing order summation code?
Posted
by
user2971559
on Stack Overflow
See other posts from Stack Overflow
or by user2971559
Published on 2013-11-09T09:49:14Z
Indexed on
2013/11/09
9:53 UTC
Read the original article
Hit count: 158
java
I want from the java to reads all numbers from the user as long as the number entered by user is bigger than the previous number. But i could write it for only positive numbers. How i can fix code below if all numbers included. If it is possible please write the solution for beginners because its my first year in computer science in college and I haven't learn a lot yet.
import acm.program.*;
public class IncreasingOrder extends ConsoleProgram {
public void run() {
int previousNumber = 0;
int total = 0;
int count = 0;
while(true) {
int n = readInt("Enter > ");
if (n <= previousNumber) break;
total += n;
count++;
previousNumber = n;
}
println("You have entered " + count + " numbers in increasing order.");
println("Sum of these " + count + " numbers is " + total + ".");
}
}
© Stack Overflow or respective owner