奇数之和

时间:2017-01-06 12:50:21

标签: java sum

你得到总和1 - 3 + 5 - 7 + 9 - 11 + 13 ...你应该编译一个程序(给定整数N)找到并显示第N个加数的和的值。

我甚至不知道这个程序应该如何。我写了一些代码,但不知道要添加什么。拜托,你能帮帮我吗? :)

这是我的代码:

Scanner input = new Scanner(System.in);

System.out.print("n = ");
int n = input.nextInt();
int sum = 0;

for (int i = 1; i <= n; i++) {       
    if (i % 2 != 0) {
        sum = sum + i;
    }
}

System.out.println(sum);

5 个答案:

答案 0 :(得分:2)

可能你想要这个

如果我输入 i / p 7 ,这将产生 -4 as o / p

for (int i = 1; i <= n; i+=2) {
        if( i % 4 == 1 )
            sum = sum + i;
        else
            sum = sum - i;
}

以@fafl样式(使用Ternary运算符),如果我错了,请纠正我

sum += (i % 2 != 0) ? ( i % 4 == 1 ) ? + i : - i;

如果我输入 i / p 7 ,这将产生 7 as o / p

        int n = input.nextInt();
        int sum = 0;
        int addOrDedduct = 1;
        for (int i = 1; i <= n; i++ ) {
                if( addOrDedduct % 4 == 1 )
                    sum = sum + addOrDedduct;
                else
                    sum = sum - addOrDedduct;
                addOrDedduct+=2;
        }
        System.out.println(sum);

<强>更新
fafl的陈述sum = n % 2 == 0 ? -n : n产生相同的o / p,在这里你不需要使用loop
忘记循环并使用fafl的答案。

答案 1 :(得分:0)

您可以使用额外的变量来检查是否必须添加或减去:

    int sum = 0;
    boolean sub = false;
    for (int i = 1; i <= n; i++) {
        if (i % 2 == 1) {
            if (sub) {
                sum -= i;
                sub = false;
            } else {
                sum += i;
                sub = true;
            }
        }
    }

这个解决方案有点hacky但它​​应该可行。

答案 2 :(得分:0)

Fafl是对的 sum = n%2 == 0? -n:n

答案 3 :(得分:0)

try
{
    //  Put your code in here
}
catch (WebException ex)
{
    //  Return any exception messages back to the Response header
    OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
    response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
    response.StatusDescription = ex.Message.Replace("\r\n", "");
    return null;
}
catch (Exception ex)
{
    //  Return any exception messages back to the Response header
    OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
    response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
    response.StatusDescription = ex.Message.Replace("\r\n", "");
    return null;
}

答案 4 :(得分:0)

使用for循环,没有模数算术:

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Image x:Name="sales" Source="SomeTime1.png" Grid.Column="0" Grid.Row="0">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Tapped="SalesTapGestureRecognizer_OnTapped" NumberOfTapsRequired="1" />
    </Image.GestureRecognizers>
</Image>

<Image x:Name="sales" Source="SomeTime2.png" Grid.Column="0" Grid.Row="1">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Tapped="PersonnelTapGestureRecognizer_OnTapped" NumberOfTapsRequired="1" />
    </Image.GestureRecognizers>
</Image>

...