使用换行符时,Node.js提示同步会重复提示 [英] Node.js prompt-sync repeats prompt when using newline characters

查看:69
本文介绍了使用换行符时,Node.js提示同步会重复提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理JavaScript分配,要求我使用提示同步在Node.JS中.直到我尝试在提示中使用换行符\n为止,它都可以正常工作,这时键入的每个字符或退格键都会导致提示重复自身.

我如何做才能使用户输入显示在新行上(此练习的要求)而没有出现此问题?

问题代码:

if (guess < answer) {
  guess = prompt("Too low!\n> ");
} else if (guess > answer) {
  guess = prompt("Too high!\n> ");
}

输出屏幕截图

解决方案

您可以尝试使用此方法.

if (guess < answer) {
  console.log("Too low!");
  guess = prompt("> ");
} else if (guess > answer) {
  console.log("Too high!");
  guess = prompt("> ");
}

如果这不起作用,则可能是您自己代码的另一部分出现了问题.如果是这样,则prompt-sync模块可能有问题,如果您认为这是有效的解决方法.

I'm working on a JavaScript assignment that requires me to use prompt-synch in Node.JS. It works fine until I try to use a newline character \n within the prompt, at which point every character or backspace typed causes the prompt to repeat itself.

What could I do to get the user input to appear on a new line (a requirement of this exercise) without this issue?

Problem code:

if (guess < answer) {
  guess = prompt("Too low!\n> ");
} else if (guess > answer) {
  guess = prompt("Too high!\n> ");
}

output screenshot

解决方案

You can try this instead.

if (guess < answer) {
  console.log("Too low!");
  guess = prompt("> ");
} else if (guess > answer) {
  console.log("Too high!");
  guess = prompt("> ");
}

If this does not work then it is likely an issue with another part of your own code. If it does, then it is likely an issue with the prompt-sync module, which makes this a valid workaround if you believe it is.

这篇关于使用换行符时,Node.js提示同步会重复提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆