objective-c - 在 Objective-C Foundation 命令行实用程序中处理用户输入

标签 objective-c xcode command-line user-input

我正在尝试制作一个在 Objective-C 命令行中求解二次公式的应用程序。到目前为止我的代码是:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]){

double a, b, c, sol1, sol2;

NSLog(@"Welcome to the quadratic solver");

NSLog(@"Please enter the value of a");
scanf("%f", &a);

NSLog(@"Please enter the value of b");
scanf("%f", &b);

NSLog(@"Please enter the value of c");
scanf("%f", &c);

sol1 = (-b + sqrt( (double)pow(b,2) - 4 * a *c) ) / 2 * a;
sol2 = (-b - sqrt( (double)pow(b,2) - 4 * a *c) ) / 2 * a;

NSLog(@"If a is: %f, b is %f, and c is %f.", a, b, c);

NSLog(@"Solution one is: %f", sol1);
NSLog(@"Solution two is: %f", sol2);

但是我的输出没有收到任何信息。

2012-01-26 17:23:53.585 test[4595:707] Welcome to the quadratic solver
2012-01-26 17:23:53.588 test[4595:707] Please enter the value of a
1
2012-01-26 17:23:56.030 test[4595:707] Please enter the value of b
3
2012-01-26 17:23:56.558 test[4595:707] Please enter the value of c
-10
2012-01-26 17:23:58.670 test[4595:707] If a is: 0.000000, b is 0.000000, and c is 0.000000.
2012-01-26 17:23:58.672 test[4595:707] Solution one is: -0.000000
2012-01-26 17:23:58.672 test[4595:707] Solution two is: -0.000000

非常感谢任何输入!

最佳答案

您的输入不一致...您正在扫描到 %f,因此您需要声明:

float a, b, c, sol1, sol2;

然后你的程序在我的机器上运行良好。

关于objective-c - 在 Objective-C Foundation 命令行实用程序中处理用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9027531/

相关文章:

python - 如何合并office文档?

iphone - 如何绘制移动的激光束?

ios - 在 objective-C 的 dispatch_async 中传递参数时出现 EXC_BAD_ACCESS

ios - 没有模拟器出现在我的 Watchkit 目标下

swift - Firebase Cocoapods - 错误信息;无法构建 Objective-C 模块 'Firebase

ios - 我如何摆脱 Xcode 中的 IOS 版本 "is partial: introduced in IOS X"警告

iphone - 如何在 UItextView 中添加 placeHolder 文本?在 iPhone SDK 中

ios - 如何将数字转换为其枚举?

python - 如何处理 "The input line is too long"错误消息?

python - 如何在另一条指令后在 python 中编写内联 for 循环?