0

# Java programming loop statements

int n =10, sum = 10; While(n--) { Sum += n--; } System. Out. Print on("sun =" + sum) ;

2 Réponses

+ 3

ok, it would be great to have more details. What is your problem? What are you trying to achieve.
I'm noticing that your while statement is wrong, what you might need is while(n-- > 0)
also, tags are a bad place for code, but is great to specify the language you use, keep that in mind for your next post!

0

Hi there, I am not quite sure what you want to achieve... I have no idea why your sum starts at 10, but so be it.
int n = 10, sum = 10;
while(n > 0){
sum += n--;
}
System.out.print("sun = " + sum);
This makes your code workable and gives you the result of 65,
if you want to decrement twice you can write it like this:
int n = 10, sum = 10;
while(n-- > 0){
sum += n--;
}
System.out.print("sun = " + sum);
}
the the result is 35. I hope this helped you somehow