简体   繁体   English

JavaScript:用户输入数字,直到输入0。确定每个数字的奇偶性,最后打印偶数和奇数的总数

[英]JavaScript:User enters numbers until he enters 0.Each numbers parity is determined and at the end,the total number of even and odd numbers is printed

I also need to use the function to test the parity of the number.我还需要使用 function 来测试数字的奇偶性。 When I enter a few numbers (1,2,3,4 and then 0) the program shows "9, 0 ".当我输入几个数字(1、2、3、4,然后是 0)时,程序会显示“9, 0”。 I'm not sure why我不知道为什么

let Even = 0;
let Odd = 0;
let a = parseInt(prompt("Enter a number:"));
while (a != 0 ) {
        a = parseInt(prompt("Enter a number:"));
        isEven(a);
            if(true){
                Even = Even + a};
            if(false){
                Odd = Odd + a};
                };
console.log(Even,Odd);

function isEven(value){
    if (value%2 == 0)
        return true;
    else
        return false;
}

Two mistakes inside your loop:循环中的两个错误:

isEven(a);
if(true){
    Even = Even + a
};
if(false){
    Odd = Odd + a
};

The first one is that you're calling isEven and not using the result.第一个是您正在调用isEven而没有使用结果。 In the if you're literally just using if(true) and if(false) , so you will always go in the first one and never in the second. if您实际上只是使用if(true)if(false) ,那么您将始终在第一个中使用 go 而在第二个中永远不会。

The second mistake is that each time you're adding the entered number a instead of 1, which is what you want if you need the amount of even/odd numbers.第二个错误是每次您添加输入的数字a而不是 1,如果您需要偶数/奇数的数量,这就是您想要的。

So, keeping your (rather unusual) structure, you would have:因此,保持您的(相当不寻常的)结构,您将拥有:

var test = isEven(a);
if(test === true){
    Even = Even + 1;
}
if(test === false){
    Odd = Odd + 1;
}

In a more traditional way, I would suggest:以更传统的方式,我建议:

if(isEven(a)){
    Even = Even + 1;
} else {
    Odd = Odd + 1;
}

Try using this.尝试使用这个。

You had some problems: 1.Suppose you type 1 the prompt will close and while first iteration will continue.您遇到了一些问题: 1.假设您键入 1,提示将关闭,而第一次迭代将继续。 You open a new prompt so 1 will never be calculated.您打开一个新提示,因此永远不会计算 1。

let a = parseInt(prompt("Enter a number:"));
while (a != 0 ) {
        a = parseInt(prompt("Enter a number:"));
  1. You check IsEven(a) but never give that value to your conditions.您检查 IsEven(a) 但从不将该值赋予您的条件。

l l

et Even = 0;
    let Odd = 0;
    let a = parseInt(prompt("Enter a number:"));
    while (a != 0 ) {
           var isEvenResult = isEven(a)
                if(isEvenResult){
                    Even += parseInt(a);
                    }
               else{
                    Odd += parseInt(a);
                    }
        a = parseInt(prompt("Enter a number:"));
                    }`enter code here`
    console.log(Even +','+ Odd);
    function isEven(value){
        if (value%2 == 0)
            return true;
        else
            return false;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户输入数字金额,然后输出最高数字javascript - user enters amount on numbers then output the highest number javascript 编写确定用户输入的两个整数是否均为奇数的函数 - Writing a function that determines if both integers a user enters are odd numbers 用户输入成对数字直到他们输入“退出”,哨兵不起作用? - user to enters pairs of numbers until they enter “quit”, sentinel not working? 接收输入数字,直到用户输入零。 然后输出最高。 javascript - Receive input numbers until the user enters a zero. Then output the highest. javascript 用户在表中输入2个数字,输出数字和总和。 输出,但不在表中? - User enters 2 numbers, output numbers and sums in table. Outputting, but not in table? JavaScript-如何在1到用户输入的数字之间创建一个数字列表(数字),然后将这些数字相乘? - JavaScript - How to Create a List of Numbers Between 1 and Whatever The User Enters (Num), Then Multiply Those Numbers Together? 如果其他数字是奇数,则返回“偶数”,而“奇数”其他数字是偶数 javascript - return “even” if others numbers are odd and “odd” the others number are even javascript 无论输入的数量是多少,当用户动态输入值时都会自动查找数字总和。如果用户输入的不是数字,则提示用户 - find sum of numbers automatically as user inputs values dynamically irrespective of number of inputs.talert user if user enters other than numbers Javascript:保持偶数和奇数随机数的数量并获得每个的总和 - Javascript: Keep count of even and odd random numbers and get the sum of each JavaScript - 在数字输入之前打印奇数和偶数 - JavaScript - Print Odd & Even numbers preceding number input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM