Small Fish 发表于 2022-7-6 10:27:26

initget是否

嗨,有人可以修改我的代码吗。我想我很接近了。我还想让它适用于用户只键入字母“n”或“n”表示否,键入字母“y”或“y”表示是的情况。
谢谢
 

(defun c:YesNo (/ ans)
(initget "YES yes NO no")
(setq ans (strcase (getstring "\nYes or No: ")

(cond
((eq ans "No")
(alert "\nYou selected 'No'")
)
((eq ans "Yes")
(alert "\nYou selected 'Yes'")
)
);cond
(princ)
);defun

lpseifert 发表于 2022-7-6 10:47:07


(defun c:YesNo (/ ans)
(initget 1 "Yes No")
(setq ans (getkword "\nYes or No: "))

(cond
((eq ans "No")
(alert "\nYou selected 'No'")
)
((eq ans "Yes")
(alert "\nYou selected 'Yes'")
)
);cond
(princ)
);defun

 
在开发者帮助中查找getkword,其中的示例与您的几乎相同

Small Fish 发表于 2022-7-6 10:54:44

谢谢ipseifert
所以是getword而不是getstring!
我遵循了getstring的示例,但getword似乎是一个更简单的选项
干杯,旧金山

Lee Mac 发表于 2022-7-6 11:01:01

前一段时间我在这里发布了一些信息:
 

;; Initget / GetK(ey)word Examples
;; © Lee Mac 2010

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

;; Note 1: On Prompt String Format

;; In all examples, to achieve correct display
;; when DYNamic Input (DYNMODE=1) is enabled,
;; prompt string must be formatted thus:

;;

;; Or, with default:

;; <Option1>

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

;; Note 2: On Global Variable Format

;; In all examples utilising global variables
;; asterisks are included in variable names to decrease
;; risk of variable names clashing with unlocalised variables
;; from other programs. Asterisks are used for no other purpose.

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

;; 1. Force User Input (initget bit 1)

(initget 1 "Alpha Beta Gamma")
(setq ans (getkword "\nChoose : "))

;; User must select a choice to continue, else
;; Esc must be used to cancel evaluation

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

;; 2. Preset Default (Alpha)

;; a)

(initget "Alpha Beta Gamma")
(setq ans (cond ( (getkword "\nChoose <Alpha>: ") ) ( "Alpha" )))

;; b)

(initget "Alpha Beta Gamma")
(setq ans (getkword "\nChoose <Alpha>: "))

(if (not ans) (setq ans "Alpha"))

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

;; 3. Dynamic Default (utlisation of Global Variable: *ans*)

;; a)

(or *ans* (setq *ans* "Alpha"))

(initget "Alpha Beta Gamma")
(setq *ans*
(cond
   (
   (getkword
       (strcat "\nChoose <" *ans* ">: ")
   )
   )
   ( *ans* )
)
)

;; b)

(if (not *ans*) (setq *ans* "Alpha"))

(initget "Alpha Beta Gamma")
(setq *ans*
(cond
   (
   (getkword
       (strcat "\nChoose <" *ans* ">: ")
   )
   )
   ( *ans* )
)
)

;; c)

(initget "Alpha Beta Gamma")
(setq *ans*
(cond
   (
   (getkword
       (strcat "\nChoose <"
         (setq *ans*
         (cond ( *ans* ) ( "Alpha" ))
         )
         ">: "
       )
   )
   )
   ( *ans* )
)
)








Small Fish 发表于 2022-7-6 11:16:34

谢谢李-这将是一个有用的“如何”未来的参考
旧金山

irneb 发表于 2022-7-6 11:18:51

首先,initget对getstring根本不起作用。您只能准确地获得用户使用getstring键入的内容,如果您添加(getstring T“…”)用户需要按Enter键,这样字符串甚至可以包含空格。您可以通过检查getstring调用后输入的文本来执行类似的操作。您可以使用通配符匹配和大小写转换,例如,如果用户键入y或y或yE或YO。。。。。或者调用(wcmatch(strcase ans)“Y*”)将返回T。。。只要文本以大写或小写Y开头就可以了。您可以进一步检查以Y开头的字母,YE然后YES:(wcmatch(strcase ans)“Y,YE,YES”)。然而,用户可能仍然会输入一些奇怪的信息,比如“Maybe”。。。在这种情况下,wcmatch行将返回nil。。。i、 e.“否”???这对吗???如果没有,那么您需要再次询问用户,直到他们键入了可以接受的内容,在这种情况下,getkword(连同initget)可以为您完成所有这些。
 
大多数其他人。。。。函数与initget一起工作(在较小或更大的程度上-请参阅开发人员帮助),即使entsel、entsel和entselp也至少与关键字一起工作。但是如果你只想从用户那里获得一些文本输入,但只需要强制输入一些单词,那么getKword(注意它不是getword,它是get关键字的缩写)是你最好的选择。

Lee Mac 发表于 2022-7-6 11:34:16

不客气,小鱼
页: [1]
查看完整版本: initget是否