Printing the sums of numbers from with only 1 loop [on hold]
up vote
-2
down vote
favorite
public class nawar {
public static void main(String args) {
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 1, 4, sumRange(1, 4));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 2, 5, sumRange(2, 5));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 47, 47, sumRange(47, 47));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", -12, 5, sumRange(-12, 5));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 7, 2, sumRange(7, 2));
}
public static int sumRange(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}
}
I want it to print these following numbers: 10 14 47 - 1 -1
But I get 10, 14, 47, -63 and 0.
How do i change and what should I do?
java
New contributor
put on hold as off-topic by Martin R, IEatBagels, Toby Speight, Mast, tinstaafl 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Martin R, IEatBagels, Toby Speight, Mast, tinstaafl
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-2
down vote
favorite
public class nawar {
public static void main(String args) {
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 1, 4, sumRange(1, 4));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 2, 5, sumRange(2, 5));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 47, 47, sumRange(47, 47));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", -12, 5, sumRange(-12, 5));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 7, 2, sumRange(7, 2));
}
public static int sumRange(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}
}
I want it to print these following numbers: 10 14 47 - 1 -1
But I get 10, 14, 47, -63 and 0.
How do i change and what should I do?
java
New contributor
put on hold as off-topic by Martin R, IEatBagels, Toby Speight, Mast, tinstaafl 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Martin R, IEatBagels, Toby Speight, Mast, tinstaafl
If this question can be reworded to fit the rules in the help center, please edit the question.
This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Mast
2 days ago
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
public class nawar {
public static void main(String args) {
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 1, 4, sumRange(1, 4));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 2, 5, sumRange(2, 5));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 47, 47, sumRange(47, 47));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", -12, 5, sumRange(-12, 5));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 7, 2, sumRange(7, 2));
}
public static int sumRange(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}
}
I want it to print these following numbers: 10 14 47 - 1 -1
But I get 10, 14, 47, -63 and 0.
How do i change and what should I do?
java
New contributor
public class nawar {
public static void main(String args) {
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 1, 4, sumRange(1, 4));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 2, 5, sumRange(2, 5));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 47, 47, sumRange(47, 47));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", -12, 5, sumRange(-12, 5));
System.out.printf("Summan av talen i intervallet %d till %d är %d%n", 7, 2, sumRange(7, 2));
}
public static int sumRange(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}
}
I want it to print these following numbers: 10 14 47 - 1 -1
But I get 10, 14, 47, -63 and 0.
How do i change and what should I do?
java
java
New contributor
New contributor
New contributor
asked 2 days ago
knashejhej
1
1
New contributor
New contributor
put on hold as off-topic by Martin R, IEatBagels, Toby Speight, Mast, tinstaafl 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Martin R, IEatBagels, Toby Speight, Mast, tinstaafl
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Martin R, IEatBagels, Toby Speight, Mast, tinstaafl 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Martin R, IEatBagels, Toby Speight, Mast, tinstaafl
If this question can be reworded to fit the rules in the help center, please edit the question.
This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Mast
2 days ago
add a comment |
This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Mast
2 days ago
This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Mast
2 days ago
This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Mast
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
This question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Mast
2 days ago