集装箱编号校验

本文介绍了一种用于验证集装箱编号正确性的算法实现。该算法通过将编号转换为大写并进行一系列数学运算来检查编号的有效性,适用于长度为11位的集装箱编号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



/**
* 集装箱类
*
* @author yaoba
* @version 创建时间 2010-6-21 上午12:16:16
*/
public class Container {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(check("AMFU8505137"));
System.out.println(check("TEXU2982987"));

}

/**
* 集装箱编号校验
*
* @param no
* @return
*/
public static boolean check(String no) {
if (no == null && no.length() != 11)
return false;
no = no.toUpperCase();
int[] wi = new int[10];
for (int i = 0; i <= 4; i++) {
char ch = no.charAt(i);
if (ch == 'A') {
wi[i] = 10;
} else if (ch >= 'V' && ch <= 'Z') {
wi[i] = ch - 52;
} else if (ch >= 'L' && ch <= 'U') {
wi[i] = ch - 53;
} else {
wi[i] = ch - 54;
}
}
for (int i = 4; i < 10; i++) {
wi[i] = Integer.parseInt(no.substring(i, i + 1));
}
int sum = 0;
for (int i = 0; i < wi.length; i++) {
sum += wi[i] * Math.pow(2, i);
}
if (no.substring(0, 4) == "HLCU")
sum -= 2; // hapaq lloyd的柜号与国际标准相差2
return sum % 11 == Integer.parseInt(no.substring(10, 11));

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值