包含javaisbn的词条
本篇文章给大家谈谈javaisbn,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
JAVA问题2
08/23/2002这个字符串一共有10个字符
string
month
=
birthday.substring(0,
2);表示从birthday这个字符串的第0位往后开始截取,截取到第二个字符赋给month,就得到了08
同样的道理string
day
=
birthday.substring(3,
5);
表示从birthday这个字符串的第3位往后开始截取,截取到第五个字符赋给day,就得到了23
获取year的就不用我说了吧
上面这位大哥说的有问题吧
string
b
=
a.split("/");这句代码语法上就有问题了
a.split()这个方法获得的是一个数组
string[]
b
=
a.split("/");
JAVA ISBN计算问题。。简单JAVA编程
你的程序主要有3个问题:
(1)没有强制用户的输入值必须为9位数字;
(2)取每位数字错误;
(3)只计算了余数,没有计算差。(详见“校验码的计算方法”)
修改后的Java程序:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author Jinchuan
*/
import java.util.Scanner;
public class Exercise03_09 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String temp;
int isbn;
//限制用户只能输入9位数字
while(true){
System.out.print("Enter the first 9-digit of an ISBN number as integer: ");
temp = input.next();
try{
isbn = Integer.parseInt(temp);
}
catch(Exception e){
continue;
}
if(temp.length() == 9)
break;
}
//isbn = 730904547;
int a = isbn / 100000000; //第1位
int b = (isbn / 10000000) % 10; //第2位
int c = (isbn / 1000000) % 10;
int d = (isbn / 100000) % 10;
int e = (isbn / 10000) % 10;
int f = (isbn / 1000) % 10;
int g = (isbn / 100) % 10;
int h = (isbn / 10) % 10;
int j = isbn % 10;
int r = 11 - (a*10 + b*9 + c*8 + d*7 + e*6 + f*5 + g*4 + h*3 + j*2) % 11 ;
if (r == 10)
System.out.print("The ISBN number is " + a + b + c + d + e + f + g + h + j + "X");
else if (r == 11)
System.out.print("The ISBN number is " + a + b + c + d + e + f + g + h + j + "0");
else
System.out.print("The ISBN number is " + a + b + c + d + e + f + g + h + j + r);
}
}
运行测试:
Enter the first 9-digit of an ISBN number as integer: 12345
Enter the first 9-digit of an ISBN number as integer: 1234567890
Enter the first 9-digit of an ISBN number as integer: 730904547
The ISBN number is 7309045475
JAVA(程式设计-ISBN)
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String message = "Input an integer with less than 10 digits.";
int n = getNumber(scanner, message);
int[] array = new int[9];
String s = String.valueOf(n);
int offset = array.length - s.length();
for (int i = offset; i array.length; ++i) {
array[i] = s.charAt(i - offset) - '0';
}
int digit10 = 0;
for (int i = 0; i array.length; ++i) {
digit10 += array[i] * (i + 1);
}
digit10 = digit10 % 11;
char checksum = (digit10 10) ? (char)('0' + digit10) : 'X';
System.out.print("ISBN: ");
for (int i = 0; i array.length; ++i) {
System.out.print(array[i]);
}
System.out.print(checksum + "\n");
}
static int getNumber(Scanner scanner, String message) {
while (true) {
System.out.println(message);
String s = scanner.next();
if (s.length() 9) {
System.out.println("Input too long.");
continue;
}
try {
int n = Integer.parseInt(s);
if (n = 0) {
System.out.println("Negative number is invalid");
continue;
}
return n;
} catch (NumberFormatException e) {
System.out.println("Invalid number.");
}
}
}
}
javaisbn的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javaisbn的信息别忘了在本站进行查找喔。
发布于:2022-12-07,除非注明,否则均为
原创文章,转载请注明出处。