数字之和(预期产出问题)

时间:2016-05-02 15:59:57

标签: java

我的程序有问题。关键是用户需要输入起始编号(例如:1)和结束编号(例如:5),输出应为1 + 2 + 3 + 4 + 5 = 15。另一方面,我得到1 + 2 + 3 + 4 + 5 +。我不确定如何解决它,任何帮助将不胜感激。谢谢!无论如何还有不返回null?我的程序一直要求我将其返回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;

        }
    }

3 个答案:

答案 0 :(得分:1)

您当前在每个数字后输出+,而是输出第一个数字,然后开始循环(但在循环中,在每个数字之前输出+ )。另外,不要忘记在循环后输出 sum。像

这样的东西
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);

但是,因为您显然需要将此结果作为String返回,您应该执行类似

的操作
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();

可能使用传递给该函数的startend值(而不是忽略它们并提示用户 >功能)。

  

我试图在return语句中将两个输出一起添加到字符串中但没有运气

上面的代码在逻辑上等同于(但效率更高)

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;

它也可以像

一样更简洁地书写
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;

,如果我们使用Java 8+, lambdas 就像

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();

答案 1 :(得分:0)

希望这会有所帮助

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);

        }
    }

输出

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

答案 2 :(得分:0)

这是一种可以满足您要求的方法:

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;
}

这是输出:

System.out.println(output(1,10));
  

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55