Reputation: 141
I am having issues with my program. The point is that the user needs to enter a starting number (ex:1) and an ending number (ex:5), the output should be 1+2+3+4+5=15. I, on the other hand, am getting 1+2+3+4+5+. I am unsure as to how to fix it, any help would be greatly appreciated. Thanks!Also is there anyway to not return null? My program keeps asking me to return it to null.
import java.util.Scanner;
public class SumOfNumbers
{
public String getSum(int start, int end)
{
int sum=0;
Scanner scanner = new Scanner(System.in) ; //Scanner used to get keyboard values
System.out.println("Enter your starting number: ");
start = scanner.nextInt(); //first number
System.out.println("Enter your ending number: ");
end =scanner.nextInt(); //second number
for(int i=start;i<=end;i++)
{
sum=sum+i; //calculating sum
scanner.close();
System.out.print(i + "+");
}
return null;
}
}
Upvotes: 0
Views: 182
Reputation: 1892
Here is a method that will do what you are asking for:
public String output(int startNum, int endNum) {
String reply = "";
int answer = 0;
for (int x = startNum; x <= endNum; x++) {
if (x == startNum) reply = "" + x;
else if (x == endNum) reply = reply + "+" + x + "=";
else reply = reply + "+" + x;
answer += x;
}
reply = reply + answer;
return reply;
}
Here is the output:
System.out.println(output(1,10));
1+2+3+4+5+6+7+8+9+10=55
Upvotes: 0
Reputation: 201537
You currently output +
after every number, instead output the first number and then start looping (but in the loop, output a +
before every number). Also, don't forget to output the sum
after your loop. Something like
int sum = start;
System.out.print(start);
for (int i = start + 1; i <= end; i++) {
System.out.print("+" + i);
sum += i;
}
System.out.println(" = " + sum);
However, since you apparently need to return this result as a String
you should be doing something like
int sum = start;
StringBuilder sb = new StringBuilder();
sb.append(start);
for (int i = start + 1; i <= end; i++) {
sb.append("+").append(i);
sum += i;
}
sb.append("=").append(sum);
// System.out.println(sb.toString());
return sb.toString();
And you should probably be using the start
and end
values you pass to the function (instead of ignoring them and prompting the user in the function).
I have tried to add both of the outputs together in the return statement by making them into strings but no luck
The code above is logically equivalent to (but more efficient then)
int sum = start;
String result = String.valueOf(start);
for (int i = start + 1; i <= end; i++) {
result = result + "+" + i;
sum = sum + i;
}
result = result + "=" + sum;
// System.out.println(result);
return result;
It could also be written more concisely like
int sum = start;
String result = String.valueOf(start);
for (int i = start + 1; i <= end; i++) {
result += "+" + i;
sum += i;
}
result += "=" + sum;
// System.out.println(result);
return result;
Or, if we're using Java 8+, with lambdas like
StringBuilder sb = new StringBuilder(String.valueOf(start));
int sum = IntStream.rangeClosed(start, end).sum();
IntStream.rangeClosed(start + 1, end).forEach(x -> sb.append("+").append(x));
sb.append("=").append(sum);
// System.out.println(sb);
return sb.toString();
Upvotes: 1
Reputation: 63
hope this will help
class SumOfNumbers
{
public void getSum(int start, int end)
{
int sum=0;
Scanner scanner = new Scanner(System.in) ; //Scanner used to get keyboard values
System.out.println("Enter your starting number: ");
start = scanner.nextInt(); //first number
System.out.println("Enter your ending number: ");
end =scanner.nextInt(); //second number
for(int i=start;i<=end;i++)
{
sum=sum+i; //calculating sum
scanner.close();
if (i==end) {
System.out.print(i );
}
else {
System.out.print(i + "+");
}
}
System.out.print("="+ sum);
}
}
output
Enter your starting number:
1
Enter your ending number:
15
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15=120
Upvotes: 0