c中输入数字的总和

时间:2019-12-10 03:04:06

标签: c

我正在使用这一行代码,我的问题是每次输入数字时,我的总和的值未更新。任何帮助将不胜感激。

use framework "Cocoa"
use scripting additions

on run -- example
    tell current application's NSApp to set its delegate to me
    -- other setup stuff
end run

on applicationDockMenu:sender -- return a menu to be added to the Dock menu
    tell (current application's NSMenu's alloc's initWithTitle:"")
        its addItem:(current application's NSMenuItem's alloc's initWithTitle:"Item One" action:"myMenuAction:" keyEquivalent:"")
        its addItem:(current application's NSMenuItem's alloc's initWithTitle:"Item Two" action:"myMenuAction:" keyEquivalent:"")
        return it
    end tell
end applicationDockMenu:

on myMenuAction:sender -- a regular action handler
    display dialog (sender's title) as text
    -- additional script stuff
end sharedAction:

预期输出:

Cocoa-AppleScript Applet

2 个答案:

答案 0 :(得分:1)

在循环的每一轮中,总和都初始化为0。这样做:

#include <stdio.h>

 int condition(int a, int sum)
 {
    int i=0;
    if (a%2 == 0)
    {
        printf("Please input ODD numbers only");
    }
    if (a%2 != 0)
    {
        sum=sum+a;
        printf("\nTotal of ODD number is %d",sum);
        return sum;
    }
    return 0;
}
int main()
{
    int i, j, n, sum=0;
    printf("Total number: ");
    scanf("%d",&j);
    i=0;
    while(i<j)
    {
        printf("\nNumber %d: ", i+1);
        scanf("%d",&n);
        sum = condition(n, sum);
        i++;
    }
    return 0;
}  

请记住,如果您的函数不无效,则返回一个值。

答案 1 :(得分:0)

您似乎想使总和成为全局(即,每次调用“ condition”都不设置为零)

// make the sum global
int sum = 0;

 int condition(int a)
 {
    if(a%2==0)
    {
        printf("Please input ODD numbers only");
    }
    if(a%2!=0)
    {
        sum=sum+a;
        printf("\nTotal of ODD number is %d",sum);
    }
}