计算适当平均值的游戏制作语言

发布于 2024-08-10 03:13:05 字数 546 浏览 10 评论 0原文

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

罪歌 2024-08-17 03:13:05

2个可能的更正:

...
if (num == -99)
{
totalNum += 99;
numItems -= 1; // <----
msg += "Total is: " +string(totalNum) +"#";
averageNum = real(totalNum) / numItems; // <----
...

2 possible corrections:

...
if (num == -99)
{
totalNum += 99;
numItems -= 1; // <----
msg += "Total is: " +string(totalNum) +"#";
averageNum = real(totalNum) / numItems; // <----
...
舟遥客 2024-08-17 03:13:05

让我们尝试以下操作:

averageNum = real(totalNum / numItems); // to get the real portion of all the result not devide real over integer

或尝试:

averageNum = totalNum / numItems; //immediately , the result may be drivin automatically as a real one.

let's try the following:

averageNum = real(totalNum / numItems); // to get the real portion of all the result not devide real over integer

or try :

averageNum = totalNum / numItems; //immediately , the result may be drivin automatically as a real one.
笑看君怀她人 2024-08-17 03:13:05

我不知道 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); with averageNum = real(totalNum) / numItems;

灼痛 2024-08-17 03:13:05

我不知道 GML,但目前,您似乎正在将除法的结果转换为实数,此时为时已晚。

我修改了你的程序,做了一些其他的改进。如果它不是有效的 GML,请原谅我。

var num, totalNum, averageNum, numItems, msg;
totalNum = 0;
numItems = 0;
num = get_integer("Please enter a number. Enter -99 to quit.", "type here");
while(num != -99)
{
    totalNum += num;
    numItems += 1;
    num = get_integer("Please enter a number. Enter -99 to quit.", "type here");
}
msg = "Total is: " + string(totalNum) + "#";
averageNum = real(totalNum) / numItems;
msg += "Average is: " + string(averageNum);
show_message(msg);

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.

var num, totalNum, averageNum, numItems, msg;
totalNum = 0;
numItems = 0;
num = get_integer("Please enter a number. Enter -99 to quit.", "type here");
while(num != -99)
{
    totalNum += num;
    numItems += 1;
    num = get_integer("Please enter a number. Enter -99 to quit.", "type here");
}
msg = "Total is: " + string(totalNum) + "#";
averageNum = real(totalNum) / numItems;
msg += "Average is: " + string(averageNum);
show_message(msg);
み青杉依旧 2024-08-17 03:13:05

不是整数或小数的问题。
当输入“-99”退出循环时,numItems 增加 1,导致结果为 (1+2)/3 = 1。
您可以像修复totalNum 一样修复此问题:

         ···
   if (num == -99)
   {
   totalNum += 99;
   numItems -= 1; //Add this to undo the extra count
   msg += "Total is: " +string(totalNum) +"#";
   averageNum = real(totalNum / numItems);
   msg += "Average is: " +string(averageNum);
   show_message(msg);
   break;
   }
}

此外,您可以删除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:

         ···
   if (num == -99)
   {
   totalNum += 99;
   numItems -= 1; //Add this to undo the extra count
   msg += "Total is: " +string(totalNum) +"#";
   averageNum = real(totalNum / numItems);
   msg += "Average is: " +string(averageNum);
   show_message(msg);
   break;
   }
}

Also, you can remove real(). It serves no purpose there cause you're converting a real to a real.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文