gpt4 book ai didi

c - 这个C语言程序有什么问题吗?

转载 作者:行者123 更新时间:2023-11-30 20:51:10 24 4
gpt4 key购买 nike

我是 C 编程的初学者,我不明白这个简单的 C 程序有什么问题。我不希望程序停止,然后我创建一个 goto 语句以使其继续。问题是程序永远不会停止(即使我按 2)。这是源代码:

    #include <stdio.h>

int
main(){

int i , num , d , times ,a ;
float next;
printf("Choose a number: ");
scanf("%i" , &num);

printf("Repeat number ... times: ");
scanf("%d" , &times);

for(i=0; i<times; i++){
printf("%i\n" , num);
}

printf("Do you want to continue? (1=Yes 2=No) :");
scanf("%a" , &next);

if(a=1){
goto jump;
}
else if(a=2){
return 0;
}
else{
printf("Invalid value");
goto error;
}
jump:
printf("Choose a number: ");
scanf("%i" , &num);

printf("Repeat number ... times: ");
scanf("%d" , &times);

for(i=0; i<times; i++){
printf("%i\n" , num);
}

printf("Do you want to continue? (1=Yes 2=No) :");
scanf("%a" , &next);

if(a=1){
goto jump;
}
else if(a=2){
return 0;
}
else{
printf("Invalid value");
goto error;
}

error:
printf("Do you want to continue? (1=Yes 2=No) :");
scanf("%a" , &next);

if(a=1){
goto jump;
}
else if(a=2){
return 0;
}
else{
printf("Invalid value");
goto error;
}
}

最佳答案

问题在于比较数字的方式。在第 21 行,您检查了用户是否要继续使用以下代码:

if(a=1){
goto jump;

比较运算符是==。使用 =,将 1 赋给变量 a。任何非 0 值都会被评估为 true,因此您的代码所做的就是将 1 赋给 a,检查 a 是否不为 0,然后跳转。

关于c - 这个C语言程序有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34372397/

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