我尝试编写这个 node.js 计算器代码有一段时间了
I have attempted to write this node.js calculator code for a while
我正在尝试在 node.js 上编写一个基本的文本功能计算器,它将提出问题并使用答案来生成解决方案。例如,它应该要求第一个数字,然后是第二个数字的操作,最后显示你的结果是..有人可以帮我开发这个代码的基础吗?我有点迷茫。
听起来您正在尝试创建一个交互式计算器。
所以你会想要这样的伪代码:
while equals_not_requested {
ask for numbers
ask for operations
}
您需要使用 while 循环,因为它会循环直到请求等于运算符。同时,您的循环循环并不断请求数字和运算符。
我认为最好的方法是将您的数字和运算符放入一个数组中,然后您可以将整个数组传送到一个 'do math' 函数中。
认为您需要与控制台互动
这是示例,它会对您有所帮助。
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What is your name ? ", function(name) {
rl.question("Where do you live ? ", function(country) {
console.log(`${name}, is a citizen of ${country}`);
rl.close();
});
});
rl.on("close", function() {
console.log("\nBYE BYE !!!");
process.exit(0);
});
作为下一步,您需要编写普通的数学函数然后调用它们。
用 Sum 函数更新
// This is required and it's part of node.js api
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Here we interact with user and ask left side digit
rl.question("Please provide left side digt ", function(leftSide) {
// After user presented digit we're asing for right side
rl.question("Now right side ", function(rightSide) {
// So we have two digits it's time to sum:
/* we're converting first and second inputs
and plusing them
*/
// as output we're getting seft + right in same way we can times, devide etc
console.log(`Sum result is ${Number(leftSide) + Number(rightSide)}`);
rl.close();
});
});
// As you see we can call different functions during operation.
rl.on("close", function() {
console.log("\nBYE BYE !!!");
process.exit(0);
});
我正在尝试在 node.js 上编写一个基本的文本功能计算器,它将提出问题并使用答案来生成解决方案。例如,它应该要求第一个数字,然后是第二个数字的操作,最后显示你的结果是..有人可以帮我开发这个代码的基础吗?我有点迷茫。
听起来您正在尝试创建一个交互式计算器。
所以你会想要这样的伪代码:
while equals_not_requested {
ask for numbers
ask for operations
}
您需要使用 while 循环,因为它会循环直到请求等于运算符。同时,您的循环循环并不断请求数字和运算符。
我认为最好的方法是将您的数字和运算符放入一个数组中,然后您可以将整个数组传送到一个 'do math' 函数中。
认为您需要与控制台互动
这是示例,它会对您有所帮助。
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What is your name ? ", function(name) {
rl.question("Where do you live ? ", function(country) {
console.log(`${name}, is a citizen of ${country}`);
rl.close();
});
});
rl.on("close", function() {
console.log("\nBYE BYE !!!");
process.exit(0);
});
作为下一步,您需要编写普通的数学函数然后调用它们。
用 Sum 函数更新
// This is required and it's part of node.js api
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Here we interact with user and ask left side digit
rl.question("Please provide left side digt ", function(leftSide) {
// After user presented digit we're asing for right side
rl.question("Now right side ", function(rightSide) {
// So we have two digits it's time to sum:
/* we're converting first and second inputs
and plusing them
*/
// as output we're getting seft + right in same way we can times, devide etc
console.log(`Sum result is ${Number(leftSide) + Number(rightSide)}`);
rl.close();
});
});
// As you see we can call different functions during operation.
rl.on("close", function() {
console.log("\nBYE BYE !!!");
process.exit(0);
});