使用带有字母字符串和用户输入的 if 语句
你好,我对编程还是个新手,在我进行的研究中使用用户输入时,有一个关于使用 if 语句的问题,我似乎找不到我做错了什么? 下面是我发布的简单乘法计算器。
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
int a ;
int b ;
int c ;
printf("\n");
printf("\n");
printf("Welcome to calculator");
printf("\n");
printf("\n");
printf("what would you like to choose for first value?");
scanf("%d", &a);
printf("\n");
printf("What would you like to input for the second value?");
scanf("%d", &b);
c = a * b;
printf("\n");
printf("\n");
printf(" Here is your product");
printf("\n");
NSLog(@"a * b =%i", c);
char userinput ;
char yesvari = "yes" ;
char novari = "no";
printf("\n");
printf("\n");
printf("Would you like to do another calculation?");
scanf("%i", &userinput);
if (userinput == yesvari) {
NSLog(@" okay cool");
}
if (userinput == novari) {
NSLog(@"okay bye");
}
返回0; }
Helllo I am still new to programing and had a question about using if statements while using user input with the research I have conducted i can't seem to find what I am doing wrong?
Below is my posted simple multiplication calculator.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
int a ;
int b ;
int c ;
printf("\n");
printf("\n");
printf("Welcome to calculator");
printf("\n");
printf("\n");
printf("what would you like to choose for first value?");
scanf("%d", &a);
printf("\n");
printf("What would you like to input for the second value?");
scanf("%d", &b);
c = a * b;
printf("\n");
printf("\n");
printf(" Here is your product");
printf("\n");
NSLog(@"a * b =%i", c);
char userinput ;
char yesvari = "yes" ;
char novari = "no";
printf("\n");
printf("\n");
printf("Would you like to do another calculation?");
scanf("%i", &userinput);
if (userinput == yesvari) {
NSLog(@" okay cool");
}
if (userinput == novari) {
NSLog(@"okay bye");
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用
%i
扫描的字符不正确,需要使用strcmp
进行比较。如果您正在查找用户的字符串,则需要使用%s
并且需要足够大的字符缓冲区来容纳输入。试试这个
You are scanning the character incorrectly with
%i
and you need to compare them usingstrcmp
. If you are looking for a string from the user you need to use%s
and you need a character buffer large enough to hold the input.Try this
我认为您正在使用错误的格式
%i
读取char
:scanf("%i", &userinput);
我认为最好使用 @NSString 而不是简单的 char (我什至不确定如果你写
char a = "asd"
在 ObjC 中会发生什么,因为你给出了一个char< /code> 一个
char[]
价值) 。在这种情况下,由于字符串是指针,你不能使用==< /code> 来比较它们
。您可以使用
isEqualToString
或isEqualTo
代替。如果您对两者之间的区别感兴趣,请查看这篇文章会有所帮助。I think you are reading a
char
using the wrong format%i
:scanf("%i", &userinput);
And I think it is a better to use @NSString instead of simple char (I am not even sure what will happen in ObjC if you write
char a = "asd"
, since you are giving achar
achar[]
value) . In that case, since strings are pointers, you cannot use==
to compare them. You could useisEqualToString
orisEqualTo
instead. If you are interested in the difference between the two, look at this post would help.在 C 中,您无法使用
==
比较字符串,因此您必须使用strcmp()
之类的函数,如下所示:使用了 bang (!)因为当两个字符串匹配时,
strcmp()
实际上返回 0。欢迎来到精彩的 C 世界!In C, you can't compare strings using
==
, so you would have to use a function likestrcmp()
, like this:The bang (!) is used because
strcmp()
actually returns 0 when the two strings match. Welcome to the wonderful world of C!