[英]A string length is not printing out correctly with 0 in front
I'm working on a program that will ask for the first 9
digits of an ISBN number ( d1d2d3d4d5d6d7d8d9
) that the user inputs and then prints out the number with a 10
th digit based on an equation:我正在开发一个程序,该程序将询问用户输入的 ISBN 编号 ( d1d2d3d4d5d6d7d8d9
) 的前9
位数字,然后根据公式打印出第10
位数字:
(d1 × 1 + d2 × 2 + d3 × 3 + d4 × 4 + d5 × 5 + d6 × 6 + d7 × 7 + d8 × 8 + d9 × 9)%11)
If the checksum is 10
, the last digit is denoted as X
according to the problem.如果校验和为10
,则根据问题将最后一位表示为X
。 If not, the last number is added onto the ISBN.如果不是,则将最后一个数字添加到 ISBN 中。 I have the program working other than when the integer begins with a 0 when entered by the user.除了用户输入的整数以 0 开头之外,我让程序正常工作。
Ex前任
Input: 113601267输入:113601267
Output: 113601267 6输出:113601267 6
Ex.前任。
Input: 013032342输入:013032342
Output: "This is an invalid ISBN Entry!"输出:“这是一个无效的 ISBN 条目!”
Here's my code:这是我的代码:
int ISBN, r, i;
String st;
Scanner keyboard;
keyboard = new Scanner(System.in);
System.out.println("Please enter the first 9 digits of your ISBN number:");
ISBN = keyboard.nextInt();
st = ("" + ISBN);
if (st.length() == 9){
r = (st.charAt(0) * 1 + st.charAt(1) * 2 + st.charAt(2) * 3 + st.charAt(3) * 4 + st.charAt(4) * 5 + st.charAt(5) * 6 + st.charAt(6) * 7 + st.charAt(7) * 8 + st.charAt(8) * 9);
if (r % 11 == 10){
System.out.println("Your ISBN Number is: " + ISBN + "X");
}
else {
System.out.println(st);
i = (ISBN * 10) + (r % 11);
System.out.println("Your ISBN Number is: " + i);
}
}
else {
System.out.println("This is an invalid ISBN entry!");
}
Where did I mess up, or what is going on?我在哪里搞砸了,或者发生了什么?
You have two bugs:你有两个错误:
You should reformat your String
to prepend 0 if needed.如果需要,您应该重新格式化您的String
以在前面添加 0。
You have Strings
.你有Strings
。 A String
is composed of char
s. String
由char
组成。 If your first ISBN digit is 1
, you'll actually do '1'*1
.如果您的第一个 ISBN 数字是1
,您实际上会执行'1'*1
。 Since '1'
is a char, you'll get the int value of the char
, which is 49
.由于'1'
是一个char,你会得到的int值char
,这是49
。 What you have to do is subtract '0' from each char to get the actual int
value of each char:您需要做的是从每个字符中减去 '0' 以获得每个字符的实际int
值:
int ISBN, r, i; String st; Scanner keyboard; keyboard = new Scanner(System.in); System.out.println("Please enter the first 9 digits of your ISBN number:"); ISBN = keyboard.nextInt(); st = String.format("%09d", ISBN); // fixes your 0-bug. if (st.length() == 9){ r = ((st.charAt(0) - '0') * 1 // fixes your computation bug. + (st.charAt(1) - '0') * 2 + (st.charAt(2) - '0') * 3 + (st.charAt(3) - '0') * 4 + (st.charAt(4) - '0') * 5 + (st.charAt(5) - '0') * 6 + (st.charAt(6) - '0') * 7 + (st.charAt(7) - '0') * 8 + (st.charAt(8) - '0') * 9); if (r % 11 == 10){ System.out.println("Your ISBN Number is: " + ISBN + "X"); } else { System.out.println(st); i = (ISBN * 10) + (r % 11); System.out.println("Your ISBN Number is: " + i); } } else { System.out.println("This is an invalid ISBN entry!"); }
} }
ISBN is more of a string than a number. ISBN 更像是一个字符串而不是一个数字。
Have you tried this?你试过这个吗?
ISBN = keyboard.nextLine();
or或者
st = keyboard.nextLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.