是否可以仅使用 1 个循环遍历 2 个值?

is it possible to loop though 2 values using only 1 loop?

我有可变变量 x 和 y,我只能使用 while(某事)循环。我想遍历 0 .. 9 和 0 .. 9。我已经用不同的 if 语句和不同的顺序做了很多尝试。这是我目前所拥有的。

open System

let mutable x = 0
let mutable y = 0

let n = 9

while x <> n && y <> n do
    Console.SetCursorPosition(x, y)
    printf "."

    // ...

Console.Read() |> ignore

这样做的正常方法是使用两个嵌套循环 - 如果您要迭代已知数量的项目(而不是无限制地迭代,直到满足某些条件),那么 for 循环会更容易:

for x in 0 .. 9 do
  for y in 0 .. 9 do
    Console.SetCursorPosition(x, y)
    printf "."

嵌套循环迭代 10 次,外循环运行嵌套循环 10 次,因此嵌套主体执行了 100 次。

如果迭代超过 100 个值,则只需一个循环即可完成此操作,即 0 .. 10*10-10 .. 99。如果您有从 099 的数字,则可以通过取 x=n/10y=n%10:[=21= 来计算 xy ]

for n in 0 .. 10 * 10 - 1 do
  let x = n / 10
  let y = n % 10
  Console.SetCursorPosition(20+x, y)
  printf "."