TypeScript中的控制台输入

时间:2015-11-22 18:52:41

标签: typescript

如何在TypeScript中接收用户的控制台输入?

例如,在Python中我会使用:

userInput = input("Enter name: ")

TypeScript中的等价物是什么?

5 个答案:

答案 0 :(得分:3)

TypeScript只为JavaScript添加了可选的静态类型和转换功能。这是一个纯粹的编译时工件;在运行时,没有TypeScript,这就是为什么这个问题是关于JavaScript的,而不是TypeScript。

如果您正在谈论从控制台接受输入,那么您可能正在谈论 node.js 应用程序。在Reading value from console, interactively中,解决方案是使用stdin:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class windowMain {

    JFrame vPrincipal;
    private JTextField tfRutaArchivo;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    windowMain window = new windowMain();
                    window.vPrincipal.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public windowMain() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        vPrincipal = new JFrame();
        vPrincipal.setTitle("Ordenar Archivo POO 4to. Cuatrimestre");
        vPrincipal.setSize(600,450);
        vPrincipal.setLocationRelativeTo(null);
        vPrincipal.setVisible(true);
        vPrincipal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar mbPrincipal = new JMenuBar();
        vPrincipal.setJMenuBar(mbPrincipal);

        JMenu mPrincipal = new JMenu("Menu");
        mbPrincipal.add(mPrincipal);

        JMenuItem miExit = new JMenuItem("Salir");
        miExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        mPrincipal.add(miExit);

        JMenu mHelp = new JMenu("Ayuda");
        mbPrincipal.add(mHelp);

        JMenuItem miAbout = new JMenuItem("Acerca de ...");
        miAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                jdAbout popAcerca;
                popAcerca = new jdAbout();
                popAcerca.setVisible(true);
            }
        });
        mHelp.add(miAbout);
        vPrincipal.getContentPane().setLayout(null);

        tfRutaArchivo = new JTextField();
        tfRutaArchivo.setBounds(21, 11, 399, 20);
        vPrincipal.getContentPane().add(tfRutaArchivo);
        tfRutaArchivo.setColumns(10);

        JButton btBuscarArchivo = new JButton("Buscar");
        btBuscarArchivo.setBounds(473, 10, 89, 23);
        vPrincipal.getContentPane().add(btBuscarArchivo);
     }
    }

答案 1 :(得分:3)

您可以使用readline节点模块。请参阅节点文档中的readline

要在TypeScript中导入readline,请使用星号(*)字符。 例如:

import * as readline from 'readline';

let rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('Is this example useful? [y/n] ', (answer) => {
  switch(answer.toLowerCase()) {
    case 'y':
      console.log('Super!');
      break;
    case 'n':
      console.log('Sorry! :(');
      break;
    default:
      console.log('Invalid answer!');
  }
  rl.close();
});

答案 2 :(得分:2)

在浏览器中,您将使用提示符:

var userInput = prompt('Please enter your name.');

在节点上,您可以使用Readline

var readline = require('readline');

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question("What do you think of Node.js? ", function(answer) {
  console.log("Thank you for your valuable feedback:", answer);
  rl.close();
});

答案 3 :(得分:0)

这实际上取决于您将哪个HTML元素用作输入元素。通常,您可以在prompt()对象的帮助下使用window来读取输入。单击“确定”后,它返回用户输入的值,单击“取消”时返回null

class Greeter {
greet() {      
          alert("Hello "+this.getName())
    }
    getName() {
        return prompt("Hello !! Can I know your name..??" );;
    }
}
let greeter = new Greeter();
let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
   (greeter.greet());
}
document.body.appendChild(button);

答案 4 :(得分:0)

受@Elie G 和@Fenton 的启发,一个随时可用的“readLine()”函数,如 swift、kotlin、C...

.container {
  background-color: #ddd;
  padding: 20px;
  width: 70em;
}

.content1 {
  position: relative;
  left: 0px;
  width: 80px;
  height: 80px;
  background-color: #5C88A3;
}

.content2 {
  position: relative;
  left: 0px;
  width: 80px;
  height: 80px;
  background-color: #736FB9;
}

.content3 {
  position: relative;
  width: 80px;
  height: 80px;
  background-color: #8547BD;
}

.content4 {
  position: relative;
  width: 80px;
  height: 80px;
  background-color: #D357BB;
}