gpt4 book ai didi

javascript - 使用node.js提示用户输入?

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:06 24 4
gpt4 key购买 nike

我正在开发一个使用 node.js 运行的 JS 项目,但我不知道如何让提示正确地用于用户输入。我从 npm 安装了它,按照步骤操作,我可以让程序提示用户输入,但无法将结果存储在变量中。

我想要的是每回合提示用户下一步的移动(上、下、左或右)并使用结果。我尝试创建一个全局 move 变量并在提示部分影响它,但它似乎不起作用。

  var Moving = function(maxMoves){

for(var n=maxMoves; n>0;){
var move;
var l;
//This would prompt for a direction (up,down,left,right)
move = prompt();
//And this would prompt for the number of tiles to advance;
l = prompt();
Direction(move,l);
n-=l;
}
};

最佳答案

使用 require('prompt')require('readline') 都是很好、简单的方法来做到这一点,但我想找到最简单的方法,而且我不关心它是同步还是异步。找到它花了我比应有的时间更长的时间,所以如果人们在进一步挖掘之前阅读本文,也许我可以节省一些时间。

给你:

# npm install readline-sync

var readline = require('readline-sync');

var name = readline.question("What is your name?");

console.log("Hi " + name + ", nice to meet you.");

免责声明:我只是传播信息,以下是我的来源:

https://teamtreehouse.com/community/how-to-get-input-in-the-console-in-nodejs

How to take in text input from a keyboard and store it into a variable?

关于javascript - 使用node.js提示用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40980475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com