如何编写这个 C++代码输入苹果迪伦
我是计算机编程新手。我需要帮助来完成这项任务。我需要将这个简单的 C++ 源代码转换为苹果 dylan 代码。这是原始的数学陈述:
Task: Input an integer number n and output the sum: 1+22+32+...+n2. Use input validation for n to be positive.
我用 C++ 写了这段代码:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n < 0) return 1;
int sum = 0;
int i = 0;
while (i <= n) sum += i*i;
cout << sum;
return 0;
}
谁能帮我把这段代码写到 apple dylan 中?
最好的祝愿, 保罗
I'm new to computer programming. I need help with this task. I need to convert this simple C++ source code into apple dylan code. This is the original mathematical statement:
Task: Input an integer number n and output the sum: 1+22+32+...+n2. Use input validation for n to be positive.
I wrote this code in C++:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n < 0) return 1;
int sum = 0;
int i = 0;
while (i <= n) sum += i*i;
cout << sum;
return 0;
}
Can anyone help me to write this code into apple dylan?
Best wishes,
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个递归解决方案:(
显然,它可以从 n < 1 的一些检查中受益。)
要运行该方法,您可以发出命令:
其输出将为“55”(1 + 4 + 9 + 16 + 25 )。
您可以创建一个像这样的 main 方法:
Here's a recursive solution:
(Obviously, it could benefit from some checking for n < 1.)
To run the method you would issue the command:
The output of which would be "55" (1 + 4 + 9 + 16 + 25).
You could create a main method like this: