【发布时间】:2018-12-06 10:22:51
【问题描述】:
我正在尝试制作一个可以读取和计算一个数字有多少位的程序,这是我目前的代码
#include <stdio.h>
#include <stdlib.h>
int main(){
int a, b=0;
printf("Hey welcome to how many digits your number has!\n");
xyz:
printf("To begin enter a Number = ");
scanf("%d",&a);
while (a!=0) {
a/=10;
++b;
}
printf("your number has %d digits\n",b);
int choice;
printf("do you want to use another number?\nIf yes enter \"1\"\nif no enter \"2\"\n");
scanf("%d",&choice);
if (choice==1){
goto xyz;
}
else if (choice==2){
return 0;
}
return 0;
}
这在第一次时效果很好,但是当我返回并重复时,似乎先前尝试的“b”值已被存储.. 如果不存储变量'b'的值并保持b = 0,我该如何重新开始?
【问题讨论】:
-
把
b=0放在xyz下面 -
不错的努力。尝试在不使用
goto的情况下考虑不同的解决方案。在代码中使用goto不是一个好习惯。快乐编码:) -
goto的用处很少。这都不是。使用循环,意大利面属于带有好酱汁的盘子。
标签: c function loops while-loop