首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图获取Alexa中的贷款金额和利率并计算EMI

试图获取Alexa中的贷款金额和利率并计算EMI
EN

Stack Overflow用户
提问于 2020-07-02 02:40:48
回答 1查看 61关注 0票数 2

我正在尝试构建一个非常基本的EMI计算器。我想把贷款金额和利率分开计算,然后做数学题。但是,在获取贷款金额并确认相同之后,该程序不会移动以获取利率。

我只能达到Alexa确认贷款金额的程度。

请帮我弄明白为什么?

代码语言:javascript
复制
const Alexa = require('ask-sdk-core');


//Launch request and welcome message.
const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speakOutput = 'Hello! Welcome to your E.M.I. Calculator. Please tell me the loan amount you want to calculate the E.M.I. for';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

//capture the loan amount, save it in local variable and confirm to user the amount.
const captureLoanAmountHandler = {
    canHandle(handlerInput){
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'captureloanamount';
    },
    async handle(handlerInput){
        const currencyName = handlerInput.requestEnvelope.request.intent.slots.currency.value;
        const loanAmount = handlerInput.requestEnvelope.request.intent.slots.loanamount.value;
        
        const speakOutput = `Ok, I have captured the loan amount as ${currencyName} ${loanAmount}.`;
 //       return handlerInput.responseBuilder.speak(speakOutput);
        
 //       return handlerInput.responseBuilder.speak(speakOutput).getResponse();
        
    }
    };
    
//Prompt user for interest rate and capture it
const captureInterestRateHandler = {
    canHandle(handlerInput){
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'captureinterestrate';
    },
    
    async handle(handlerInput){
        let interestRateCaptured = false;
        while (!interestRateCaptured){
            const speakOutput = 'Please tell me the interest rate';
               interestRateCaptured = true; 
        return handlerInput.responseBuilder
            .speak(speakOutput).getResponse();
        }    
            
        const iRate = handlerInput.requestEnvelope.request.intent.slots.roi.value;    
        const speakOutput1 = `Ok, I have captured the interest rate as ${iRate}`;
        return handlerInput.responseBuilder.speak(speakOutput1).getResponse();
    }
}```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-02 07:55:01

欢迎来到stackoverflow

您的代码中有一些错误,我将逐一描述/修复它们。

您的CaptureLoanAmountHandler以简单的.speak命令结尾,这意味着Alexa将在她完成您要求她发言的句子后立即结束会话。为了使会话保持打开状态,将.reprompt添加到响应生成器(您也可以使用带有false参数的.shouldEndSession,但从UX的角度来看.reprompt更好),并为用户如何与您的技能交互添加了一些线索:

代码语言:javascript
复制
//capture the loan amount, save it in local variable and confirm to user the amount.
const CaptureLoanAmountHandler = {
    canHandle(handlerInput){
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'captureloanamount';
    },
    async handle(handlerInput){
        const currencyName = handlerInput.requestEnvelope.request.intent.slots.currency.value;
        const loanAmount = handlerInput.requestEnvelope.request.intent.slots.loanamount.value;
        
        const speakOutput = `Ok, I have captured the loan amount as ${currencyName} ${loanAmount}. Now tell me your interest rate`;
        
        return handlerInput
                 .responseBuilder
                 .speak(speakOutput)
                 .reprompt('Say: interest rate is...')
                 .getResponse();
        
    }
};

您的CaptureInterestRateHandler不应该包含while循环。在您的代码中,它将只运行一次,因为您在第一次运行时将保护设置为true ;)

代码语言:javascript
复制
//Prompt user for interest rate and capture it
const CaptureInterestRateHandler = {
    canHandle(handlerInput){
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'captureinterestrate';
    },
    
    async handle(handlerInput){
        const iRate = handlerInput.requestEnvelope.request.intent.slots.roi.value;    
        const speakOutput1 = `Ok, I have captured the interest rate as ${iRate}`;
        return handlerInput.responseBuilder.speak(speakOutput1).getResponse();
    }
}

我想当您收集所有输入数据时,应该会有一些计算。

根据你的评论:

代码语言:javascript
复制
//capture the loan amount, save it in local variable and confirm to user the amount.

我想您以后会在其他意图处理程序中看到贷款金额的价值--恐怕不会:所有变量,甚至const在单个处理程序运行中都是可用的。为了在其他意图中访问它们,您需要将它们存储在SessionAttributes中。

除此之外,看看更接近对话框 -扰流板警报-它只是做所有与对话相关的魔术幕后,并在最后,您得到您的值您的要求;)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62688137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档