英文:
CMD stops after pressing enter key to obtain code result
问题
/*
国际标准书号(ISBN)是一种在每本书上打印的唯一数字书籍标识符。
ISBN基于一个10位数字代码。如果满足以下条件,ISBN就是合法的:
1*第1位数字+2*第2位数字+3*第3位数字+4*第4位数字+5*第5位数字+6*第6位数字+7*第7位数字+8*第8位数字+9*第9位数字+10*第10位数字 可以被11整除。
例如:对于ISBN 1401601499,Sum=1*1+2*4+3*0+4*1+5*6+6*0+7*1+8*4+9*9+10*9 (=254),可以被11整除。
编写一个程序来:
(i) 输入一个10位的ISBN码作为整数。
(ii) 如果输入的ISBN不是一个10位整数,则输出消息 "非法的ISBN" 并终止程序。
(iii) 如果数字可以被11整除,则输出消息 "合法的ISBN"。如果不能被11整除,则输出消息 "非法的ISBN"。
*/
import java.util.*; // 包名
class BookNumber {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("输入ISBN:");
long isbn = sc.nextLong();
long r;
long d = 0;
long s = 0;
long num = isbn;
while (num != 0) // 数字计数逻辑
{
++d;
num = num / 10; // 移除最后一位数字
}
if (d == 10)
{
while (isbn != 0)
{
r = num % 10; // 提取最后一位数字
s = s + r * d--;
num = num / 10;
}
if (s % 11 == 0)
{
System.out.println("合法的ISBN");
} else {
System.out.println("非法的ISBN");
}
}
} // 主函数结束
} // 类结束
英文:
This is a program based on a book identifier no. Now, when I run it in CMD and input ISBN, then after pressing enter CMD stops and doesn't show the result. Moreover it stops taking any other enter image description here input. Below is my code and link to screenshot of CMD. Thanks.
/*
The international standard book number(ISBN) is a unique numeric book identifier which is printed on every book.
THe ISBN is based upon a 10-digit code. THe ISBN is legal if:
1*digit1+2*digit2+3*digit3+4*digit4+5*digit5+6*digit6+7*digit7+8*digit8+9*digit9+10*digit10 is divisible by 11.
Exaample : for an ISBN 1401601499,Sum=1*1+2*4+3*0+4*1+5*6+6*0+7*1+8*4+9*9+10*(=254 Which is divisible by 11.
Write a program to:
(i)Input the ISBN codes as a 10-digit integre
(ii)Input the ISBN is not a 10-digit integer,output message "Illegal ISBN" and terminate the program.
(iii)If the number is divisible by 11 output the message"Legal ISBN". If the sum is not divisible by 11,output the message "Illegal ISBN".
*/
import java.util.*; // package name.
class BookNumber {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the ISBN : ");
long isbn = sc.nextLong();
long r;
long d = 0;
long s = 0;
long num = isbn;
while (num != 0) //digit count logic
{
++d;
num = num / 10; //it remove the last digit
}
if (d == 10)
{
while (isbn != 0)
{
r = num % 10; //it extract the last digit
s = s + r * d--;
num = num / 10;
}
if (s % 11 == 0)
{
System.out.println("Legal ISBN");
} else {
System.out.println("Illegal ISBN");
}
}
} // End of main
} //End of class
答案1
得分: 1
我认为你的 while (isbn != 0)
是一个无限循环。如果你不在循环体内改变 isbn
变量,它将会无限运行。
英文:
I think your while (isbn != 0)
is an infinite loop. If you don't change the isbn
variable inside the body, it will run indefinitely.
答案2
得分: 0
你需要在第二个循环的开始处将 num
重置为 isbn
。
当ISBN的长度不是10位时,应该提供错误消息。
import java.util.*;
public class BookNumber {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("输入ISBN:");
long isbn = sc.nextLong();
long r;
long d = 0;
long s = 0;
long num = isbn;
while (num != 0) //数字计数逻辑
{
++d;
num = num / 10; //移除最后一位数字
}
if (d == 10) {
num = isbn;
while (num != 0) {
r = num % 10; //提取最后一位数字
s = s + r * d--;
num = num / 10;
}
if (s % 11 == 0) {
System.out.println("合法的ISBN");
} else {
System.out.println("非法的ISBN");
}
} else {
System.out.println("非法的ISBN");
}
} // 主方法结束
} // 类结束
英文:
You need to reset num
to isbn
at the start of the second loop.
Also you should give an error message when the length of the ISBN is not 10.
import java.util.*; // package name.
public class BookNumber {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the ISBN : ");
long isbn = sc.nextLong();
long r;
long d = 0;
long s = 0;
long num = isbn;
while (num != 0) //digit count logic
{
++d;
num = num / 10; //it remove the last digit
}
if (d == 10)
{
num = isbn;
while (num != 0)
{
r = num % 10; //it extract the last digit
s = s + r * d--;
num = num / 10;
}
if (s % 11 == 0)
{
System.out.println("Legal ISBN");
} else {
System.out.println("Illegal ISBN");
}
} else {
System.out.println("Illegal ISBN");
}
} // End of main
} //End of class
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论