【发布时间】:2019-06-30 23:33:05
【问题描述】:
我正在尝试编写一个不使用“pow”函数来计算 X^Y(X 的 Y 次方)的 C 程序。当我输入数字时,程序运行良好,但是当用户输入不是数字的字符时,我遇到了代码没有停止的问题。在给出“您输入的字符不是数字。请重试。您输入的字符不是数字。请重试。值为 1。”消息后,它再次运行程序。有人可以帮忙吗?想弄清楚这一点我快疯了。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, y, i,n=1,val;
printf("Please enter a number: \n");
scanf("%d", &x);
if (x!=1)
printf("The character you have entered is not a number.Please try again.");
else
printf("What power is the number raised to?\n");
scanf("%d", &y);
if (y!=1)
printf("The character you have entered is not a number.Please try again.");
else
for(i=1;i<=y;i++)
{
val=x;
n=n*val;
}
printf("The value is %d.\n", n);
return 0;
}
【问题讨论】:
标签: c if-statement scanf