2016-09-26 76 views
1

我正在编写一个程序,该程序将要求用户输入的第一个9数字的ISBN编号(d1d2d3d4d5d6d7d8d9),然后用10打印出该编号基于下述等式个数位:字符串长度未正确打印,前面为0

(d1 × 1 + d2 × 2 + d3 × 3 + d4 × 4 + d5 × 5 + d6 × 6 + d7 × 7 + d8 × 8 + d9 × 9)%11) 

如果校验10,最后一位数字是根据该问题表示为X。如果不是,最后一个数字将被添加到ISBN上。除了当用户输入整数以0开头时,我的程序工作。

输入:113601267
输出:113601267

输入:013032342
输出: “这是一个无效的ISBN条目”

这里是我的代码:

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!"); 
} 

哪儿了我搞砸了,或者是怎么回事?

+4

you're要求的'int'所以你得到的'int'。一个数字不关心前导零,并将它们分开。你可以使用'keyboard.nextLine()'或'keyboard.next()'来开始。 – SomeJavaGuy

+0

我可以,但在它的问题说:编写一个程序,提示用户输入前9位数字,并显示包括前导零的10位ISBN, 。 **您的程序应读取输入为整数** – bordia

+0

您的ISBN是一个字符串而不是数字。领先的'0'事项,而一个数字将放弃这些。你应该用'scanner.next()'将它作为一个字符串读取,而不是将它转换为数字,然后返回到String中。 –

回答

1

你有两个错误:

  1. 你应该重新格式化您如果需要String前面加上0。

  2. 您有Strings。 A Stringchar组成。如果你的第一个国际标准书号是1,你实际上会做'1'*1。由于'1'是char,因此您将获得char的int值,即49。你所要做的是减法“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!"); 
    } 
    

    }

+0

它的所有运行很好,罚款,除了st.length()。如果整数以0开头,我把它变成一个字符串,它跳到我的else语句。也许它有一个整数带0的东西,但我被告知我需要输入是一个整数。 – bordia

+0

ISBN不是“int”(或“long”或任何数字值)。告诉你的老师。 ISBN是数字“字符串”值。 –

+0

谢谢你的修复!我做了你所说的,我只是让它打印字符串而不是ISBN整数。问题似乎是固定的。 – bordia

0

ISBN更是一个字符串超过了许多。

你试过这个吗?

ISBN = keyboard.nextLine(); 

st = keyboard.nextLine(); 
+0

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine() – redolent