Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 整数未保存在循环中/部分工作_Javascript_Node.js_Loops_Readline - Fatal编程技术网

Javascript 整数未保存在循环中/部分工作

Javascript 整数未保存在循环中/部分工作,javascript,node.js,loops,readline,Javascript,Node.js,Loops,Readline,我是javascript新手,目前正在编写一个在终端中运行的基本文件。它的大部分都在发挥作用。它允许用户添加/删除信用并检查其信用。最后一部分是允许他们购买一种产品,并将该金额从他们的整体信贷中扣除 我遇到的问题是,虽然它确实显示购买物品时金额已被删除。当返回菜单并检查剩余的信用后,它会回到原来的金额 我尝试使用下面的代码来保存信用卡(即使在检查可用信用卡后也会这样做): 但由于数组使用用户输入的数字,因此它将从总积分中删除该数字,而不是从实际价格中删除该数字。因此,例如,如果用户选择选项4,它

我是javascript新手,目前正在编写一个在终端中运行的基本文件。它的大部分都在发挥作用。它允许用户添加/删除信用并检查其信用。最后一部分是允许他们购买一种产品,并将该金额从他们的整体信贷中扣除

我遇到的问题是,虽然它确实显示购买物品时金额已被删除。当返回菜单并检查剩余的信用后,它会回到原来的金额

我尝试使用下面的代码来保存信用卡(即使在检查可用信用卡后也会这样做):

但由于数组使用用户输入的数字,因此它将从总积分中删除该数字,而不是从实际价格中删除该数字。因此,例如,如果用户选择选项4,它将从信用卡中删除4,而实际上糖果是3英镑


如果您能提供任何帮助,我将不胜感激,因为我花了很长时间没有找到解决方案。

问题是,在他们购买产品后,您从未更新信用卡 下面这一行所做的只是他们希望你做对的程度:)

解决方案非常简单,更新信用,然后显示信用,例如:

if (index == 3) {
    credit = credit - 4;
    console.log('Thank you, your Candy has now been dispensed. Your total credit is now £' + credit);
}
所以你的代码应该是

 var readlineSync = require('readline-sync');
 var credit = 0;
 var removeCredit = 0;

 menu = [];
 menu[0] = "Purchase a product";
 menu[1] = "View your credit";
 menu[2] = "Add credit";
 menu[3] = "Retrieve a refund";
 menu[4] = "Quit!";

 products = [];
 products[0] = "Drink: £1";
 products[1] = "Crisps: £1";
 products[2] = "Chocolate: £2";
 products[3] = "Candy: £3";

 do {
     index = readlineSync.keyInSelect(menu, 'Please choose your option');
     if (index == 1) {
         console.log("The total amount of credit you have is: £", credit);
     }

     if (index == 2) {
         credit += readlineSync.questionInt('How much credit would you to purchase? ');
         console.log("The total amount of credit you have is: £" + credit);
     }

     if (index == 3) {
         credit -= readlineSync.questionInt('How much credit would you like to remove? ');
         console.log("This credit has now been removed, your total available credit is: £" + credit);
     }
     if (index == 0) {
         index = readlineSync.keyInSelect(products, 'What product would you like?');
         if (index == 0) {
            credit = credit - 1;
            console.log('Thank you, your Drink has now been dispensed. Your total credit is now £' + credit);
         }
         if (index == 1) {
            credit = credit - 1;
            console.log('Thank you, your Crisps have now been dispensed. Your total credit is now £' + credit);
         }
         if (index == 2) {
            credit = credit - 2;
            console.log('Thank you, your Chocolate has now been dispensed. Your total credit is now £' + credit);
         }
         if (index == 3) {
            credit = credit - 3;
            console.log('Thank you, your Candy has now been dispensed. Your total credit is now £' + credit);
         }
     }
 } while (index != 4)

我对您的代码进行了一些清理,并修复了您观察到的问题:

var readlineSync = require('readline-sync');
var credit = 0;
var index;

var menu = [
    'Purchase a product',
    'View your credit',
    'Add credit',
    'Retrieve a refund',
    'Quit!'
];

var products = [
    'Drink',
    'Crisps',
    'Chocolate',
    'Candy'
];

var prices = [1, 1, 2, 3];

var productList = products.map(function(product, i) {
    return product + ': £' + prices[i];
});

do {
    index = readlineSync.keyInSelect(menu, 'Please choose your option');

    if (index == 1) {
        console.log('The total amount of credit you have is: £%d', credit);
    }

    if (index == 2) {
        credit += readlineSync.questionInt('How much credit would you to purchase? ');
        console.log('The total amount of credit you have is: £%d', credit);
    }

    if (index == 3) {
        credit -= readlineSync.questionInt('How much credit would you like to remove? ');
        console.log('This credit has now been removed, your total available credit is: £%d', credit);
    }

    if (index == 0) {
        index = readlineSync.keyInSelect(productList, 'What product would you like?');

        if (index >= 0 && index < products.length) {
            credit -= prices[index];
            console.log('Thank you, your %s has now been dispensed. Your total credit is now £%d', products[index], credit);
        }

        continue;
    }

} while(index != 4);
此函数遍历
products
中的值,对于每个值
product
和每个索引
i
,它以
product:.price
的格式返回一个新字符串,并将其存储到数组
productList
的相应索引中

  • 我将您的
    console.log
    语句更改为使用内联格式以使其更清晰
    %s
    表示在此处放置字符串,
    %d
    表示在此处放置整数。然后使用以下参数填充这些格式说明符

  • 我在上一个
    if
    块中添加了一个
    continue
    语句,以便尽早返回循环。这是因为
    索引
    被您选择的产品覆盖,因此它不再反映原始菜单选择


  • 如果您对我的反馈有任何疑问,请随时发表评论。

    Ahh感谢您澄清我的错误所在!它现在工作正常。再次感谢!感谢您花时间清理代码,最重要的是解释这些更改所做的工作。这有助于澄清我的理解,所以我感谢你们!这只是一个友好的提醒,如果答案解决了您的问题,通常会将其标记为正确答案,因为这样做既有助于您的声誉,也有助于我们的声誉。
     var readlineSync = require('readline-sync');
     var credit = 0;
     var removeCredit = 0;
    
     menu = [];
     menu[0] = "Purchase a product";
     menu[1] = "View your credit";
     menu[2] = "Add credit";
     menu[3] = "Retrieve a refund";
     menu[4] = "Quit!";
    
     products = [];
     products[0] = "Drink: £1";
     products[1] = "Crisps: £1";
     products[2] = "Chocolate: £2";
     products[3] = "Candy: £3";
    
     do {
         index = readlineSync.keyInSelect(menu, 'Please choose your option');
         if (index == 1) {
             console.log("The total amount of credit you have is: £", credit);
         }
    
         if (index == 2) {
             credit += readlineSync.questionInt('How much credit would you to purchase? ');
             console.log("The total amount of credit you have is: £" + credit);
         }
    
         if (index == 3) {
             credit -= readlineSync.questionInt('How much credit would you like to remove? ');
             console.log("This credit has now been removed, your total available credit is: £" + credit);
         }
         if (index == 0) {
             index = readlineSync.keyInSelect(products, 'What product would you like?');
             if (index == 0) {
                credit = credit - 1;
                console.log('Thank you, your Drink has now been dispensed. Your total credit is now £' + credit);
             }
             if (index == 1) {
                credit = credit - 1;
                console.log('Thank you, your Crisps have now been dispensed. Your total credit is now £' + credit);
             }
             if (index == 2) {
                credit = credit - 2;
                console.log('Thank you, your Chocolate has now been dispensed. Your total credit is now £' + credit);
             }
             if (index == 3) {
                credit = credit - 3;
                console.log('Thank you, your Candy has now been dispensed. Your total credit is now £' + credit);
             }
         }
     } while (index != 4)
    
    var readlineSync = require('readline-sync');
    var credit = 0;
    var index;
    
    var menu = [
        'Purchase a product',
        'View your credit',
        'Add credit',
        'Retrieve a refund',
        'Quit!'
    ];
    
    var products = [
        'Drink',
        'Crisps',
        'Chocolate',
        'Candy'
    ];
    
    var prices = [1, 1, 2, 3];
    
    var productList = products.map(function(product, i) {
        return product + ': £' + prices[i];
    });
    
    do {
        index = readlineSync.keyInSelect(menu, 'Please choose your option');
    
        if (index == 1) {
            console.log('The total amount of credit you have is: £%d', credit);
        }
    
        if (index == 2) {
            credit += readlineSync.questionInt('How much credit would you to purchase? ');
            console.log('The total amount of credit you have is: £%d', credit);
        }
    
        if (index == 3) {
            credit -= readlineSync.questionInt('How much credit would you like to remove? ');
            console.log('This credit has now been removed, your total available credit is: £%d', credit);
        }
    
        if (index == 0) {
            index = readlineSync.keyInSelect(productList, 'What product would you like?');
    
            if (index >= 0 && index < products.length) {
                credit -= prices[index];
                console.log('Thank you, your %s has now been dispensed. Your total credit is now £%d', products[index], credit);
            }
    
            continue;
        }
    
    } while(index != 4);
    
    var productList = products.map(function(product, i) {
        return product + ': £' + prices[i];
    });