使用带有字母字符串和用户输入的 if 语句

发布于 2024-11-25 17:17:59 字数 896 浏览 1 评论 0原文

你好,我对编程还是个新手,在我进行的研究中使用用户输入时,有一个关于使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走走停停 2024-12-02 17:17:59

您使用 %i 扫描的字符不正确,需要使用 strcmp 进行比较。如果您正在查找用户的字符串,则需要使用 %s 并且需要足够大的字符缓冲区来容纳输入。

试试这个

//Make sure userinput is large enough for 3 characters  and null terminator
char userinput[4];

//%3s limits the string to 3 characters
scanf("%3s", userinput);

//Lower case the characteres
for(int i = 0; i < 3; i++)
    userinput[i] = tolower(userinput[i]);

//compare against a lower case constant yes
if(strcmp("yes", userinput) == 0)
{
    //Logic to repeat
    printf("yes!\n");
}
else
{
    //Lets just assume they meant no
    printf("bye!\n");
}

You are scanning the character incorrectly with %i and you need to compare them using strcmp. 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

//Make sure userinput is large enough for 3 characters  and null terminator
char userinput[4];

//%3s limits the string to 3 characters
scanf("%3s", userinput);

//Lower case the characteres
for(int i = 0; i < 3; i++)
    userinput[i] = tolower(userinput[i]);

//compare against a lower case constant yes
if(strcmp("yes", userinput) == 0)
{
    //Logic to repeat
    printf("yes!\n");
}
else
{
    //Lets just assume they meant no
    printf("bye!\n");
}
浊酒尽余欢 2024-12-02 17:17:59

我认为您正在使用错误的格式 %i 读取 charscanf("%i", &userinput);

我认为最好使用 @NSString 而不是简单的 char (我什至不确定如果你写 char a = "asd" 在 ObjC 中会发生什么,因为你给出了一个 char< /code> 一个 char[] 价值) 。在这种情况下,由于字符串是指针,你不能使用==< /code> 来比较它们。您可以使用 isEqualToStringisEqualTo 代替。如果您对两者之间的区别感兴趣,请查看这篇文章会有所帮助。

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 a char a char[] value) . In that case, since strings are pointers, you cannot use == to compare them. You could use isEqualToString or isEqualTo instead. If you are interested in the difference between the two, look at this post would help.

情绪少女 2024-12-02 17:17:59

在 C 中,您无法使用 == 比较字符串,因此您必须使用 strcmp() 之类的函数,如下所示:

if ( !strcmp(userinput, yesvari) ) {
   //etc.
}

使用了 bang (!)因为当两个字符串匹配时,strcmp() 实际上返回 0。欢迎来到精彩的 C 世界!

In C, you can't compare strings using ==, so you would have to use a function like strcmp(), like this:

if ( !strcmp(userinput, yesvari) ) {
   //etc.
}

The bang (!) is used because strcmp() actually returns 0 when the two strings match. Welcome to the wonderful world of C!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文