gpt4 book ai didi

java - UPC 代码问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:44:48 26 4
gpt4 key购买 nike

我一直在试图找出为什么我的输出不是它应该的样子。给出的示例是

Enter a UPC (enter a blank line to quit): 036000291453 Check digit should be: 2 Check digit is: 3 UPC is not valid

Enter a UPC (enter a blank line to quit): 036000291452 Check digit should be: 2 Check digit is: 2 UPC is valid

Enter a UPC (enter a blank line to quit): 014633149077 Check digit should be: 4 Check digit is: 7 UPC is not valid

Enter a UPC (enter a blank line to quit): 014633149074 Check digit should be: 4 Check digit is: 4 UPC is valid

Enter a UPC (enter a blank line to quit): 0853911765722 ERROR! UPC MUST have exactly 12 digits

Enter a UPC (enter a blank line to quit): 085391176572 Check digit should be: 2 Check digit is: 2 UPC is valid

Enter a UPC (enter a blank line to quit): Goodbye!

获得该输出的算法是这样的:从左到右,将奇数位置的数字相加(从1开始计数),并将结果乘以3。从左到右,将偶数位置上的数字与步骤 1 中计算出的总数相加获取步骤 2 的结果并计算除以 10 后的余数(结果模 10)。如果余数不为零,则用 10 减去该余数即可得到校验位。如果余数为零,则校验位应为0。

    String str1 = validinput(in);
int odd1 = odd(str1);
int even1 = even(str1);
int f = (odd1+even1)%10;
if(f != 0){
f = 10-f;
}
System.out.println(odd1);
System.out.println(even1);

System.out.println("Check digit should be: "+f);
System.out.println("Check digit is: "+str1.charAt(11));
int y = Character.getNumericValue(str1.charAt(11));
if (f == y){
System.out.println("UPC is valid");
}
else{
System.out.println("UPC is not valid");
}
}


private static String validinput(Scanner inScanner){

System.out.print("Enter a UPC (enter a blank line to quit): ");
String str = inScanner.nextLine();


while(str.length() != 12){
if (str.length() == 0){
System.out.println("Goodbye");
break;
}
else{
System.out.println("ERROR! UPC MUST have exactly 12 digits");

System.out.print("Enter a UPC (enter a blank line to quit): ");
str = inScanner.nextLine();
}
}
return str;
}


private static int odd(String input){
int i = 1;
char ch;
int sumOdd = 0;
while (i < 11){
ch = input.charAt(i);
int x = Character.getNumericValue(ch);
sumOdd = x +sumOdd;
i += 2;
}
int Mx3=sumOdd*3;
return Mx3;
}


private static int even(String input){
int i = 0;
char ch;
int sumEven = 0;
while (i < 11){
ch = input.charAt(i);
int x = Character.getNumericValue(ch);
sumEven = x +sumEven;
i += 2;
}

return sumEven;
}

最佳答案

charAt() 使用基于 null 的索引,但指令希望您使用基于 1 的索引。因此,在 odd() 中从 i = 0 开始。在 Even() 中从 i = 1 开始。

其次,您使用 Character.getNumericValue(ch) 获取字符的 unicode 代码点值,但说明要求您使用数字值。因此,请改用 Integer.parseInt(ch.ToString())

private static int odd(String input){
int i = 0;
char ch;
int sumOdd = 0;
while (i < 11){
ch = input.charAt(i);
int x = Integer.parseInt(ch.ToString());
sumOdd = x +sumOdd;
i += 2;
}
int Mx3=sumOdd*3;
return Mx3;
}


private static int even(String input){
int i = 1;
char ch;
int sumEven = 0;
while (i < 11){
ch = input.charAt(i);
int x = Integer.parseInt(ch.ToString());
sumEven = x +sumEven;
i += 2;
}

return sumEven;
}

代码未测试

关于java - UPC 代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42450795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com