用户输入数字对,直到他们输入“退出",前哨不起作用? [英] user to enters pairs of numbers until they enter "quit", sentinel not working?

查看:55
本文介绍了用户输入数字对,直到他们输入“退出",前哨不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要解决的问题:编写一个程序,要求用户输入数字对,直到他们输入"quit"为止.输入并验证每对数字后,请使用函数添加数字.该函数将有两个数字对参数,并将返回总和.用户输入退出"后,输出所有数字对及其和.

Here's the problem I'm trying to solve: Write a program that asks a user to enters pairs of numbers until they enter "quit". As each pair of numbers is entered and validated, add the numbers using a function. The function will have two parameters for the pair of numbers and will return the sum. After the user enters "quit", output all the pairs of numbers and their sums.

我只有一次执行一个程序就可以输出数字1,数字2和总和,但是当我尝试重复直到用户输入退出"时,我似乎会中断它?

I've got the program to output number1, number2 and the sum when I just do one, but when I try to repeat until the user enters "quit" I seem to break it?

//function to sum 2 entered numbers
function sum2enteredNumbers(number1, number2) 
{
    var sum1and2;
    sum1and2 = number1 + number2;
    return sum1and2;
}


function exercise4Part1() {
    // PART 1: YOUR CODE STARTS AFTER THIS LINE
    var QUIT_CODE = "quit";
    var output;
    var number1;
    var number2;

    while (number1 !== QUIT_CODE || number2 !== QUIT_CODE)
    {
    number1 = Number(prompt("Enter a number:"));
    number2 = Number(prompt("Enter another number:"));     
    }


    sum1and2 = sum2enteredNumbers(number1, number2);

    output = document.getElementById('outputPart1');

    output.innerHTML = "<br />Entry 1: " + number1 + " Entry 2: " + number2 + " Sum: " + sum1and2;

}

尝试2仍然不起作用:

function sum2enteredNumbers(number1, number2) 
{
    var sum1and2;
    sum1and2 = number1 + number2;
    return sum1and2;
}


function exercise4Part1() {
    // PART 1: YOUR CODE STARTS AFTER THIS LINE
    var QUIT_CODE = "quit";
    var output;
    var number1;
    var number2;


    while (number1 !== QUIT_CODE && number2 !== QUIT_CODE)
    {
        number1 = prompt("Enter a number or \"quit\":");
        number2 = prompt("Enter another number or \"quit\":"); 
        if (number1 !== QUIT_CODE && number2 !== QUIT_CODE)
        {
         number1 = Number(number1);
         number2 = Number(number2);
        }
    }

    sum1and2 = sum2enteredNumbers(number1, number2);

    output = document.getElementById('outputPart1');

    output.innerHTML = "<br /> Entry 1: " + number1 + " Entry 2: " + number2 + " Sum: " + sum1and2;

}

推荐答案

从概念上讲,您想要这样做:

Conceptually, you want to do this:

  1. 获取用户输入,
    • 如果在任何时候输入都是退出",则停止.(哨兵支票)

对于第1步,您已完成大部分工作.考虑以下功能:

For step 1, you're most of the way there. Consider this function:

function getInput(msg) { 
    var value = prompt(msg); 
    if (value === QUIT_CODE) { 
         return false; 
    }
    return value; 
}

然后您可以在 while 条件下调用此函数,同时仍将输入分配给 number1 number2 .

You can then call this function in the while condition, while still assigning the input to number1 or number2.

while ((number1 = getInput('Enter a number')) &&
       (number2 = getInput('Enter another number'))) { 
    // convert and output 
}

为什么这样做?

  • && || ,即JavaScript的逻辑布尔运算符短路,这意味着条件为 false 而不评估如果&& 的第一个操作数为false,并且第二个操作数的条件为 true ,而第一个操作数为 true .
  • 通常会在JavaScript中将 0 视为错误,这将导致while条件失败,并且如果用户输入0,则无法正常工作.但是,在这种情况下,输入的来源是字符串,表达式'0'是正确的.
  • &&, || , i.e. JavaScript's logical boolean operators short circuit, which means the condition will be false without evaluating the second operand if the first operand is false for &&, and the condition will be true without evaluating the second operand if the first operand is true.
  • One would normally think of 0 as falsey in JavaScript which would cause the while condition to fail and not work if the user entered 0. However in this case, the source of the input is a string, and the expression '0' is truthy.

这篇关于用户输入数字对,直到他们输入“退出",前哨不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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