Javascript教程
JavaScript基础
JavaScript Objects
JavaScript BOM
JavaScript DOM
JavaScript OOP
JavaScript Cookies
JavaScript事件
JavaScript异常
JavaScript常用

JavaScript 程序检查数字是否具有相同的最后一位数字

用于检查数字是否具有相同最后一位数字的 JavaScript 程序

在本例中,您将学习编写一个程序,在 JavaScript 中检查三个数字的最后一位数字是否相同。
要理解此示例,您应该了解以下JavaScript 编程主题:
JavaScript 比较和逻辑运算符 JavaScript if...else 语句

示例: 检查最后一位

/* program to check whether the last digit of three
numbers is same */
// take input
const a = prompt('Enter a first integer: ');
const b = prompt('Enter a second integer: ');
const c = prompt('Enter a third integer: ');
// find the last digit
const result1 = a % 10;
const result2 = b % 10;
const result3 = c % 10;
// compare the last digits
if(result1 == result2 && result1 == result3) {
    console.log(`${a}, ${b} and ${c} have the same last digit.`);
}
else {
    console.log(`${a}, ${b} and ${c} have different last digit.`);
}
输出
Enter a first integer: 8
Enter a second integer: 38
Enter a third integer: 88
8, 38 and 88 have the same last digit.
在上面的例子中,要求用户输入三个整数。
三个整数值存储在变量 abc 中。
整数值的最后一位是使用模运算符 %计算的。
% 给出余数。例如,58 % 10 给出 8。
然后使用 if..else 语句和逻辑 AND 运算符 && 运算符比较所有最后一位数字。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4