计算适当平均值的游戏制作语言
这是我的代码:
var num, totalNum, averageNum, numItems, msg;
msg = "";
totalNum = 0;
numItems = 0;
while(true)
{
num = get_integer("Please enter a number. Enter -99 to quit.", "type here");
totalNum += num;
numItems += 1;
if (num == -99)
{
totalNum += 99;
msg += "Total is: " +string(totalNum) +"#";
averageNum = real(totalNum / numItems);
msg += "Average is: " +string(averageNum);
show_message(msg);
break;
}
}
现在,如果我输入数字 1 和 2,然后输入 -99 来打破循环,我会得到平均值 1。我想要得到实数,而不是整数。有什么帮助吗?
Here is my code:
var num, totalNum, averageNum, numItems, msg;
msg = "";
totalNum = 0;
numItems = 0;
while(true)
{
num = get_integer("Please enter a number. Enter -99 to quit.", "type here");
totalNum += num;
numItems += 1;
if (num == -99)
{
totalNum += 99;
msg += "Total is: " +string(totalNum) +"#";
averageNum = real(totalNum / numItems);
msg += "Average is: " +string(averageNum);
show_message(msg);
break;
}
}
Right now, if I enter the number 1 and 2 and then -99 to break the loop, I get an average of 1. I want to get the real number, not the integer. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
2个可能的更正:
2 possible corrections:
让我们尝试以下操作:
或尝试:
let's try the following:
or try :
我不知道 Game Maker Language 是如何工作的。
但是,请尝试更换
averageNum = real(totalNum / numItems);
与averageNum = real(totalNum) / numItems;
I don't know how Game Maker Language works.
However, try replacing
averageNum = real(totalNum / numItems);
withaverageNum = real(totalNum) / numItems;
我不知道 GML,但目前,您似乎正在将除法的结果转换为实数,此时为时已晚。
我修改了你的程序,做了一些其他的改进。如果它不是有效的 GML,请原谅我。
I don't know GML but at the moment, you seem to be converting the result of the division to a real, and by this time it is too late.
I have revised your program, making some other improvements. Forgive me if it's not valid GML.
不是整数或小数的问题。
当输入“-99”退出循环时,numItems 增加 1,导致结果为 (1+2)/3 = 1。
您可以像修复totalNum 一样修复此问题:
此外,您可以删除real()。它在那里没有任何作用,因为您正在将实数转换为实数。
Is not an integer or decimal problem.
When you enter "-99" to exit the loop, numItems increases by 1, causing the result to be (1+2)/3 = 1.
You could fix this the same way you fixed totalNum:
Also, you can remove real(). It serves no purpose there cause you're converting a real to a real.