简体   繁体   English

图灵语言中的计数循环

[英]Counted loops in turing language

% calculates the population of a city from year 2000 to 2020

var popstart : int := 80000
var popgrowth : real
var popend : real
var growthrate : real := 0.03

% popgrowth := popstart * growthrate
    for i : 2000..2020 by 1
popgrowth := popstart * growthrate
end for

put "year  population"
put "====  =========="
put  i, "  ", popgrowth

when I run the program, i get the error variable "i has not been declared" when I declare i as a variable, I get the error "i has already been declared" 当我运行程序时,我将错误变量“ i尚未声明”时,将错误声明为“我已被声明”

the output should look something like this: 输出应如下所示:

year population 年人口
==== ========== ==== ==========
2000 xxxxxxxxxx ~ 2020 XXXXXXXXXX 2000 xxxxxxxxxx〜2020 XXXXXXXXXX

here's a similar, but much simpler program, where i was successful in doing what i'm attempting to do in the program above. 这是一个类似但更简单的程序,在该程序中我成功完成了上面程序中的尝试。

for i : 4 .. 19 by 3
    put i
end for

HELP PLEASE! 请帮助! and thanks in advance! 并预先感谢!

You have to declare i. 你必须申报我。 i is a variable that starts from 2000 and goes up to 2020. i是一个从2000开始到2020年的变量。

var i: int should do the trick. var i:int应该可以解决问题。

Another answer, knowing even less about Turing.... I'm guessing that i is only in scope during the loop since that is where it is declared, so then it is throwing the error when you try to use i outside the loop at the end. 另一个答案,对Turing的了解甚至更少。...我猜i只是在循环期间处于作用域内,因为这是声明的地方,所以当您尝试在循环外使用i时,它将引发错误结束。 Which explains why the smaller program you posted works but the larger one doesn't. 这就解释了为什么您发布的较小的程序有效而较大的程序无效的原因。 And also why it would throw an error when you try to declare i first outside the loop and then declare it again in the loop statement. 还有当您尝试在循环外首先声明i然后在循环语句中再次声明i时为什么会引发错误的原因。

I believe that is the same thing that Wolever was getting at, but didn't really explain why. 我相信这与Wolever的想法相同,但并未真正解释原因。 (His answer should fix it if we're right about i only being valid in the loop) (如果我们正确地说i仅在循环中是有效的,他的答案应该可以解决)

Your main problem is that i is out of scope. 您的主要问题是i超出了范围。 The i can only be used within the for loop. i只能在for循环内使用。 Put the put statements within the for loop. put语句放入for循环中。 Like this: 像这样:

% calculates the population of a city from year 2000 to 2020

var popstart : real := 80000
var popgrowth : real
var popend : real
var growthrate : real := 0.03

% popgrowth := popstart * growthrate
for i : 2000..2020
    popgrowth := popstart * growthrate
    popstart += popgrowth
    put "year  population"
    put "====  =========="
    put  i, "  ", popstart
end for

@DavidWolever's Solution would only produce last year's population growth. @DavidWolever的解决方案只会带来去年的人口增长。 Your program also has a logical error, in that it would only give the population growth rate, and not the total population. 您的程序还有一个逻辑错误,因为它只会给出人口增长率,而不是总人口增长率。

I assume that the problem has been resolved with your teachers, because Turing is very much an obsolete language. 我认为您的老师已经解决了这个问题,因为图灵是一种过时的语言。 It lost support years ago. 几年前它失去了支持。 I still remember some from ninth grade, but it may not be a good idea to post Turing related questions here, as nobody really understands the language and it's conventions. 我仍然记得九年级的一些,但是在这里发布与Turing相关的问题可能不是一个好主意,因为没人真正了解该语言及其惯例。

Resources for help : http://touque.ca/EC/programming/Turing/ 帮助资源: http : //touque.ca/EC/programming/Turing/

A bit more nitpicky detail that is unrelated: the for statement is not properly indented, and the by 1 is unnecessary. 一点无关紧要的细节: for语句未正确缩进,而by 1则不必要。

I don't know much about Turing, but I suspect that the for i ... is an implicit declaration of i . 我不知道很多关于图灵,但我怀疑, for i ...是一个隐含的声明i

So, I don't know how you could fix it, but you could get around it by doing this: 因此,我不知道如何解决它,但是您可以通过以下方法解决它:

var last_year: int
for i : 2000..2020 by 1
    popgrowth := popstart * growthrate
    last_year = i
end for

put "year  population"
put "====  =========="
put  last_year, "  ", popgrowth
% calculates population growth for city of Whitby between 2000 and 2020.

var popstart : int := 80000
var popgrowth : real
var growthrate : real := 0.03

put "year  population"
put "====  =========="
put "2000  80000"

popgrowth := popstart
for i : 2001 .. 2020 by 1
    popgrowth := popgrowth + (popgrowth * growthrate)
    put i, "  ", popgrowth:0:2
end for

I don't how to explain,but you can figure out whether this is right or not.(I make some change in your content.) 我不解释,但您可以弄清楚这是否正确。(我对您的内容进行了一些更改。)

var popstart : int := 80000
var popgrowth : real
var popend : real
var growthrate : real := 0.03
var number:real
% popgrowth := popstart * growthrate
    for i : 2000..2020 by 1
    number:=i
popgrowth := popstart * growthrate
end for

put "year  population"
put "====  =========="
put  number, "  ", popgrowth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM