Sum of numbers
I am supposed to write a program that asks the user for a positive non-zero integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. (if a user enters 5 the program would add 1,2,3,4,5 and print out 15.)
Yesterday I wrote this code:
import javax.swing.JOptionPane;
publicclass Test
{
publicstaticvoid main(String[] args)
{
int done = 0;
do{
String input;
double n;
double s = 0;
input = JOptionPane.showInputDialog("Enter a number: ");
n = Double.parseDouble(input);
s = ((1+number)*(number/2));
if (n < 1)
{
JOptionPane.showMessageDialog(null,"The number you have entered" +
" is not a positive nonzero" +
" integer value.");
}
else
{
JOptionPane.showMessageDialog(null, s);
}
}while (done == 1);
}
}
but my professor said that I should use increment or decrement operators (++ --) instead of using my formula:
s = ((1+number)*(number/2))
So today I wrote:
import javax.swing.JOptionPane;
publicclass Testtwo
{
publicstaticvoid main(String[] args)
{
int number;
int sum;
String input;
input = JOptionPane.showInputDialog("Enter a number: ");
number = Integer.parseInt(input);
while (number < 1)
{
input = JOptionPane.showInputDialog("The number must be a POSITIVE " +
"NONZERO integer value: ");
number = Integer.parseInt(input);
}
sum = ((number) + (number--));
JOptionPane.showMessageDialog(null, sum);
}
}
I don't get any errors, and when I type in 3, I get the correct answer of 6 (1+2+3 = 6) but when I type in 5, I get an answer of 10.
Any insight to what I am doing wrong would be greatly appreciated.
[3220 byte] By [
wksmdta] at [2007-10-3 9:22:25]

>I don't get any errors, and when I type in 3, I get the correct answer of 6 (1+2+3 = 6)
That was just luck.
>but when I type in 5, I get an answer of 10.
This line: sum = ((number) + (number--));
which can be written without all the parentheses, like this:
sum = number + number--
Says add number to number and store the result in sum, and then decrement number. So, if number equals 3, those steps are:
sum = 3 + 3
number = number -1
and if number equals 5, the steps taken by that line of code are:
sum = 5 + 5
number = number - 1
To get the result you want, you can use a for loop that loops from 1 to less than or equal to number. Use a variable called sum, and add i to it every time through the loop.
hi wksmdt,
when you use the -- (postdecrement) Operator in your term number + number--, it is applied AFTER adding number + number. Maybe try this
while (number > 0) {
sum += number--;
}
E.g. number = 5
After 1. Loop sum = 5, number =4,
after 2. Loop sum = 9, number = 3,
after 3. Loop sum = 12, number = 2,
after 4. Loop sum = 14, number = 1,
after 5. Loop sum = 15, number = 0
end of loop.
import javax.swing.JOptionPane;
public class Testtwo
{
public static void main(String[] args)
{
int number = 1;
int sum = 0;
String input;
input = JOptionPane.showInputDialog("Enter a number: ");
number = Integer.parseInt(input);
while (number < 1)
{
input = JOptionPane.showInputDialog("The number must be a POSITIVE " +
"NONZERO integer value: ");
number = Integer.parseInt(input);
}
while (number > 0) {
sum += number--;
}
JOptionPane.showMessageDialog(null, sum);
}
}
Is there a quicker/more efficient way to write this? It will be on my next hand written exam, and It seems difficult to remember.