用于计算x提高Y的X功能的程序
int main()
{ unsigned int x, y;
printf_s("Enter value for x: ");
scanf_s("%u", &x);
printf_s("Enter value for y: ");
scanf_s("%u", &y);
unsigned int count = 1;
while (count < y);
{
x *= x;
count++;
}
printf_s("x raised to the power of y is: %u", x);
}
嗨,我不确定我在哪里出错。 当我运行它时,我会按照提示输入两个值,然后什么也没有发生。 我输入y的价值之后就停止了。
输入x:3的值 输入y:2
这样的值。有人可以将我指向正确的方向吗? 我知道,如果y&lt; = 1,这种方式将无法工作。 但是当y&gt时,它不应该适合; 1?
我已经搜索了。还有另一个问题用于循环。 我可以看到它可以用于循环,但我认为虽然循环更合适,因为它可以提供更多的自由。 请,谢谢!
int main()
{ unsigned int x, y;
printf_s("Enter value for x: ");
scanf_s("%u", &x);
printf_s("Enter value for y: ");
scanf_s("%u", &y);
unsigned int count = 1;
while (count < y);
{
x *= x;
count++;
}
printf_s("x raised to the power of y is: %u", x);
}
Hi, I'm not sure where I went wrong.
When I run it, I enter two values as prompted and then nothing else happens.
It just stops after I enter they value for y.
Enter value for x: 3
Enter value for y: 2
Like this. Could someone point me in the right direction?
I understand that this way will not work if y <= 1.
But shouldn't it work for when y > 1?
I've searched for it. There is another question using for loop.
I can see that it could be done with for loop but I think while loop is more appropriate since it gives more freedom.
Please and thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
;
在上使用,在循环中未执行您的代码,x *= x
肯定是错误的。https://godbolt.org/z/xzx8mo5aw
;
past thewhile
and your code in braces is not executed in the loopx *= x
is definitely wrong.https://godbolt.org/z/xzx8Mo5aW
我已经删除了;
我添加了很长的时间,以供未签名的int。
我已经设置了一个新的变量,以包含X升至Y的X的值。
我已经检查了当y = 0或1时是否有效,并且确实如此。
谢谢大家的帮助!
PS教科书答案具有pritnf(“%s”,“输入第一个整数:”);要提示第一个整数,x。我不确定为什么它们会添加%s,因为没有它,它的工作正常。有人知道为什么会添加%s吗?
I've deleted ;.
I've added long to unsigned int.
I've set up a new variable, power, in order to contain the value of x raised to a power of y.
I've checked if this works when y = 0 or 1 and it does.
Thank you everyone for your help!
P.S. Textbook answer has pritnf( "%s", "Enter first integer: "); for prompting for the first integer, x. I'm not sure why they add %s as it works perfectly fine without it. Does anyone know why one would add %s?