本帖最后由 darkpiece 于 2014-2-17 22:00 编辑
class YanZhengMa
{
public static void main(String[] args)
{
System.out.println(checkEN("6921317905038"));
}
/*
需求4:编程验证EN13条码真伪
"6921317905038"
计算13条码前12位奇数位数字的和,sum1
计算13条码前12位偶数位数字的和,sum2
int sum = sum1+sum2*3
将结果%10,int r = sum%10;
int result = 10-r;
return result==(code.charAt(12)-'0');
返回值类型:boolean
参数:String
*/
public static boolean checkEN(String code)
{
int sum1 =0;//奇数位的和
int sum2 =0;//偶数位的和
for(int i=0;i<12;i++)
{
int num =code.charAt('i')-'0';
if(i%2==0)
{
sum1+=num;
}else
{
sum2+=num;
}
}
int sum = sum1+sum2*3;
int c = 10-sum%10;
return c==(code.charAt(12)-'0');
}
}
帮忙分析分析 |
|