shailujp 发表于 2022-7-6 06:07:41

Can';t退出WHILE循环

大家好,
 
我被困在while循环中,无法摆脱它。有人能帮我吗(如果可能的话,请提供为什么它没有关闭的信息)。这只是我正在使用的一个更大的lisp的一个小函数,退出Esc意味着完整的程序终止。
 
以下是我如何使用它的。一旦用户决定使用drawshapes,如果答案是关键字之一(例如矩形),则用户应该能够绘制他喜欢的任意多个矩形,如果按Enter键,则应再次返回主菜单,要求选择关键字(圆形或椭圆)。
 
提前谢谢。
 

(defun drawshapes (/ dtype)
      (setvar "cmdecho" 1)
      (setq dtype T)
      (while dtype
      (initget (+ 2 4) "R P C E")
      (prompt "\n***    Enter option R, P, C, E   ***")
(setq dtype (getkword "\nselect your option: Rectangle, Pline, Circle, Ellipse or hit Enter to close:"))
(if dtype
               
   (cond
    ( (= dtype "R")
            (setq loop T)
            (while loop   
         (command "_.rectang")
                  (while (= 1 (logand 1 (getvar 'CMDACTIVE)))(command pause))
            ); end while
    )
    ( (= dtype "P")
            (setq loop T)
            (while loop
                  (command "_.PLINE")
         (while (= 1 (logand 1 (getvar 'CMDACTIVE)))(command pause))
            ); end while
         )
    ( (= dtype "C")
            (setq loop T)
            (while loop
                  (command "_.circle")
                  (while (= 1 (logand 1 (getvar 'CMDACTIVE)))(command pause))
            ); end while
    )
    ( (= dtype "E")
            (setq loop T)
            (while loop
         (command "Ellipse")
         (while (= 1 (logand 1 (getvar 'CMDACTIVE)))(command pause))
            ); end while
    )
   ); end cond
); end if
   ); end while
   (setvar "cmdecho" 0)
);end defun

Bhull1985 发表于 2022-7-6 06:15:07

(当数据类型

(如果是数据类型?
不是一个大师能告诉你为什么和确切的原因,但这些是我的第一个想法,也是我要研究的

shailujp 发表于 2022-7-6 06:20:26

我已经在getkword行下面有了(if-dtype)。这不是同一件事吗?我尝试将while改为if,并给出了语法错误。
 
我想我有太多的时间Setq的数据类型。不知道为什么。。。我很困惑

David Bethel 发表于 2022-7-6 06:22:00

在某些情况下,您必须将loop设置为nil。
 
此外,您可以使用:
(while (setq dtype (getkword "\n....

 
 
-大卫

David Bethel 发表于 2022-7-6 06:27:16

此外,AFAIK,getkword实际上在(initget)调用中只使用1或0。
 
-大卫

Bhull1985 发表于 2022-7-6 06:29:53

如果你在大多数情况下使用while,我会在while上看到一个增量计数器。。

(setq cntr 1)
actions
(setq cntr (1+ cntr))
        ;increment the counter

或者像大卫说的那样,你可以(setq dtype nil)
在每个(=dtype)语句之后。这将在使用一次后退出循环,但它表示您希望他们能够进入其中许多形状。。。因此,您可以根据更多用户输入设置标志。
 
注意你的代码中:

(setq loop T)
            (while loop

 
 
这意味着是的,它们确实应该留在这个循环中,直到你添加一些关闭/退出循环的东西,通常是(setq loop nil),对于你在那里得到的东西。
hth公司

Bhull1985 发表于 2022-7-6 06:33:30

这里还有一个片段,我使用initget和strcase的组合来获得正确的用户输入。initget函数还将确保用户从预定义列表中进行选择,因此我觉得测试用户输入没有正常必要

(initget "Freeze Thaw On oFF Lock Unlock eXit")      ;; establish controls
(setq lstate (strcase (getkword "\nChoose layer control :")))

 
我不确定initget中的算术是否真的在做任何事情,因为只要在下半部分加上一个(strcase)就可以对给定的选择进行任何大小写,因此。。。。只有在做出正确选择时,prog才会继续,而不在initget中使用算术。我不确定这是否或多或少是正确的,但它对我的目的很有效

Bhull1985 发表于 2022-7-6 06:40:54

另一个例子,这一项归功于Jeffery p sanders和他的importxyz。lsp。只是摘录一下,展示一些现有的方法

   ;;;--- Inform the user of progress
   (if(> cellCnt 99)
       (progn
         (princ "\n Currently retrieving cells in Row ")
         (princ (strcat (itoa stRow) " of " (itoa LsRow)))
         (setq cellCnt 0)
       )
   )
   ;;;--- Increment the column
   (setq stCol(+ stCol 1))
   ;;;--- Make sure the column stays in the range
   (if(> stCol LsCol)
       (setq stCol copyCol stRow(+ stRow 1))
   )
   )
   ;;;--- Return the list
   cellList
)

 
但是,请密切关注其中的变量。。

Bhull1985 发表于 2022-7-6 06:42:30

再举一个例子,希望能有所帮助。。。
(这是Jeff Tippit,SPAUG总裁,www.SPAUG.org的lisp)

   (setq DO_MORE "Y")
   (while (= DO_MORE "Y")
      (BIG_LOOP)
   ); End while

   (defun BIG_LOOP ()
      (USER_INPUT)
      (setq FRAMUS_DONE 0)   
      (while (/= FRAMUS_DONE 1)
         (SHOW_DIALOG)
      ); End while
      (I_OR_G)
      (initget "Y N")
      (setq DO_MORE (getkword "\nChange more blocks? Y/N <Y>: "))
         (if
            (or
               (= DO_MORE "y")
               (= DO_MORE "")
               (= DO_MORE nil)
            ); End or
               (setq DO_MORE "Y")
         ); End if
   ); End BIG LOOP

在上面的这个例子中,你可以看到他是如何接受用户输入并根据用户选择决定是否继续程序的

neophoible 发表于 2022-7-6 06:46:48

我同意循环变量会让你永远停留在时间里,除非你找到改变它的方法。总的来说,你的方法似乎在正确的轨道上。
 
我更喜欢在用户眼中看起来更像自动驾驶。这是一个编辑,它充实了您的选项,添加了一个显式的退出选项,并且需要一个简单的回车/回车来重复最后一个选项。它还需要一个额外的变量,该变量应添加到本地列表中。未经测试。
 
在快速回顾之后,我发现我的cond实际上并不必要,因为如果dtype=“eXit”,循环不应该走那么远。哎呀!
页: [1] 2
查看完整版本: Can';t退出WHILE循环