gpt4 book ai didi

c - 难以理解循环

转载 作者:太空宇宙 更新时间:2023-11-04 03:44:26 25 4
gpt4 key购买 nike

我是 C 编程的新手,我很难掌握如何在 C 编程中使用 whileif 函数。

目前我正在粗略地尝试启动我的程序。我目前在我的“if 语句”处收到错误消息“预期表达式”,但我认为代码有更多错误,因为它不会执行我正在查找的内容。

理想情况下,我想这样做,以便用户将继续收到相同的错误消息,直到他们正确输入 1-3 之间的数字,然后一旦他们输入正确的数字,它就会存储它并继续接下来的步骤。抱歉,我对此很陌生,我一直对我正在阅读的有关循环和 if 语句的所有不同教程感到困惑。

如有任何帮助,我们将不胜感激。

#include <stdio.h>

int main(void)
{
printf("Welcome to the ROBOT GAME!\n"
"To help you visualize draw a three by three board on a piece of paper.\n"
"Please enter the column of your starting point.\n"
"Keep numbers in the range of one to three.\n");
int a;
scanf("%i", &a);
while(a<1||a>3)
{
printf("Error: Sorry try again.\n"
"Please enter the column of your starting point.\n");
int a;
scanf("%i", &a);
break;

if(a=>1 && a<=3)
printf("Please enter the row of your starting point. Keep numbers in the range of one to three.\n");

int b;
scanf("%i", &b);
}
}

最佳答案

尽管您的程序是错误的,但错误消息意味着您必须在 if 语句的条件中将 =>= 替换为 >=

if(a=>1 && a<=3)

那是一定有

if(a >= 1 && a<=3)
^^^^

代码看起来像

#include <stdio.h>

int main( void )
{
printf( "Welcome to the ROBOT GAME!\n"
"To help you visualize draw a three by three board on a piece of paper.\n"
"Please enter the column of your starting point.\n"
"Keep numbers in the range of one to three.\n" );
int a;

scanf( "%i", &a );

while ( a < 1 || a > 3 )
{
printf( "Error: Sorry try again.\n"
"Please enter the column of your starting point.\n" );

scanf( "%i", &a );
}

int b;

printf( "Please enter the row of your starting point. Keep numbers in the range of one to three.\n" );

scanf( "%i", &b );

while ( b < 1 || b > 3 )
{
printf( "Error: Sorry try again.\n"
"Please enter the row of your starting point.\n" );

scanf( "%i", &b );
}

return 0;
}

关于c - 难以理解循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com