无法找到要使用><的循环 [英] cant find which loop to use ><

查看:102
本文介绍了无法找到要使用><的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,询问用户在选择程序后是否希望使用大写或小写字母,然后打印所选的选项,这是我到目前为止所拥有的...

Hi, I am trying to write a program which asks the user if they would like the alphabet written in upper or lower case after they make the choice the program with then print the chosen choice, here is what I have so far...

#include <stdio.h>

int main ()
{
    char string1[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char string2[27] = "abcdefghijklmnopqrstuvwxyz";
    int value = 0;

    printf("To print the alphabet in\n");
    printf("uppercase enter 0\n");
    printf("lowercase enter 1\n");
    scanf("%i",&value);

    if (value != 0||1)
        printf("Invalid entry please enter only 0 or 1");
    else if (value = 0)
        printf("%s", string1);
    else if (value = 1);
        printf("%s", string2);

    return 0;
}

推荐答案

#include< stdio.h>
int main()
{

#include <stdio.h>
int main ()
{

char string1 [27] ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char string2 [27] ="abcdefghijklmnopqrstuvwxyz";
int值= 0;

char string1[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char string2[27] = "abcdefghijklmnopqrstuvwxyz";
int value = 0;

printf(要在\ n中打印字母");
printf(大写字母输入0 \ n");
printf(小写字母输入1 \ n");
scanf("%i,& value);

printf("To print the alphabet in\n");
printf("Uppercase enter 0\n");
printf("lowercase enter 1\n");
scanf("%i",&value);

while((value!= 0)&& value!= 1)
{
printf(无效的输入,请仅输入0或1 \ n");
scanf(% i \ n,& value);
}

while ((value != 0) && value != 1)
{
printf("Invalid entry please enter only 0 or 1\n");
scanf("%i\n",&value);
}

if(值== 0)
{
printf(%s",string1);
}
否则if(值== 1)
{
printf (%s",字符串2);
}
返回0;

if (value == 0)
{
printf("%s", string1);
}
else if (value == 1)
{
printf("%s", string2);
}
return 0;

}

这是我得到的,在某种程度上可以正常工作,但是由于某种原因,例如输入9然后0,它需要我输入另一个int,因此出于某种原因,我看不到

this is what i got which works to an extent but for some reason after entering e.g 9 then 0 it needs me to enter another int so for some reason which i cant see


您想要做的更多的是逻辑上的问题.您试图在输入为0或1时捕获输入.您的逻辑看"起来就像是良好的逻辑,但实际上并非如此.

What you are trying to do is more of a logical issue. You are attempting to catch input when it is a 0 or a 1. Your logic ''Looks'' like good logic, but really it isn''t.

让我解释一下你在说什么:

Let me explain what you are saying:

如果(值!= 0 || 1)

if (value != 0||1)

这是什么意思,如果变量值不等于undefined,则执行此操作.

What this means is this, If the variable value is not equal to undefined then do this.

这是什么意思

如果变量值不等于0并且变量值不等于1,则执行此操作.

If the variable value is not equal to 0 AND the variable value is not equal to 1 then do this.

简而言之,您将||弄糊涂了或比较器.您需要进行2次测试,但您只需进行一次.

Simply put you are confusing the || or comparitor. You need to have 2 tests, but you only have one.

关于循环,这是我认为您想要的示例.

As to the Loop, here is an example of what I think you wanted.

#include

"stdafx.h"

"stdafx.h"

int _tmain(int argc,_TCHAR * argv [])

int _tmain(int argc, _TCHAR* argv[])

{

///创建两个字符串数组,一个上数组,一个下数组.

//Create two string arrays, one upper, one lower.

char string1 [27] ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char string1[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char string2 [27] ="abcdefghijklmnopqrstuvwxyz";

char string2[27] = "abcdefghijklmnopqrstuvwxyz";

//创建一个整数以捕获用户输入.

//Create an integer to catch the user input.

int值= 0;

//创建一个循环以提示用户.

// create a loop to prompt the user.

(值== 0)

{

printf(以打印字母\ n);

printf(To print the alphabet in\n");

printf(大写输入1 \ n");

printf("uppercase enter 1\n");

printf(小写输入2 \ n");

printf("lowercase enter 2\n");

scanf(%i",& value);

scanf("%i",&value);

}//while循环结束

} //end of while loop

//如果输入不是1且输入不是2,请测试输入

// Test the input, if the input is not 1 and input is not 2,

if(((value!= 1)&&(value!= 2))

if ((value != 1)&&( value != 2))

{

printf(无效输入,请仅输入0或1");

printf("Invalid entry please enter only 0 or 1");

}

//如果输入为1,则发送大写字符串.

// If the input is a one, send the Upper Case String.

否则if(值== 1)

else if (value == 1)

{

printf(%s",string1);

printf("%s",string1);

}

//如果输入为两个,则发送小写字符串.

// If the input is two, send the Lower Case String.

否则if(值== 2)

else if (value == 2)

{

printf(%s",string2);

printf("%s",string2);

}

//退出程序.

返回0;

}


该行显示:
scanf("%i\n",&value);

应该说:

scanf("%i",&value);


这篇关于无法找到要使用&gt;&lt;的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆