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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9027531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:06:45  来源:igfitidea点击:

Working with user input in Objective-C Foundation command line utility

objective-cxcodecommand-lineuser-input

提问by user1172534

I'm trying to make an application that solves the quadratic formula in Objective-C command line. My code so far is:

我正在尝试制作一个应用程序来解决 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);

}

}

But my output is not receiving any of the information.

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

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

Any input is greatly appreciated!

非常感谢任何输入!

采纳答案by Conrad Shultz

You have inconsistent typing... you are scanning into %f, so you need to declare:

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

float a, b, c, sol1, sol2;

Your program then works fine on my machine.

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