我需要使用字符串,字符,一堆嵌套循环和条件语句为我的CS类构建此ISBN校验和生成器(对于ISBN-10和ISBN-13)。在混乱中的某个地方,我认为某些事情会触发无限循环,因为当系统提示我输入信息时,我给出输入并按Enter键,它会转到新的一行,并希望我输入更多的数据,而我猜测每次成功输入后,它应该提示我再次输入另一个,否则告诉我这是不正确的,然后再次要求另一次输入。而且,当我键入quit时,它并不会像预期的那样结束程序并显示校验和结果,而是表现出与其他输入相同的行为。如果我第一次输入quit而没有给程序提供任何数字,则它确实会正确结束程序,但是输出变量的值当然为null。
到目前为止,我的代码:
/******************************************************************************
* Program Name: Lab05A - ISBN
* Program Description: Calculate ISBN-10 AND ISBN-13
* Program Author: xxxxxxxxx
* Date Created: 10/10/2018
* Change# Change Date Programmer Name Description
* ------- ------------ ------------------- ---------------------
******************************************************************************/
package lab05a;
import java.util.Scanner;
public class Lab05A {
public static void main(String[] args) {
// Input for s
Scanner input = new Scanner(System.in); // Create new scanner
System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: "); // our ever-lasting prompt
String s = input.next(); // declare string variable "s" and set it equal to next input from user.
String output10 = null; // Declaring string output10
String output13 = null; // Declaring string output13
// main while loop
while (!"QUIT".equals(s)) { //this will run as long as the program does not receive an input of "QUIT", not case sensitive.
char checkDigit;
char checkSum = '0';
if (s.length() == 9) { //if the length of the inputted string is 9 characters...
int sum = 0; // initialize sum variable
for (int i=0; i <= s.length();) {
sum = sum + ((s.charAt(i) - '0') * (i + 1));
}
if (sum % 11 == 10) {
checkDigit = 'X';
}
else {
checkDigit = (char) ('0' + (sum % 11));
}
output10 = output10 + "\n" + s + checkDigit;
System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");
s = input.next();
}
else if (s.length() == 12) {
int sum = 0;
for (int i=0; i <= s.length();) {
if (i % 2 == 0) {
sum = sum + (s.charAt(i) - '0');
}
else {
sum = sum + (s.charAt(i) - '0') * 3;
}
checkSum = (char) (10 - sum % 10);
if (checkSum == 10) {
checkSum = 0;
}
output13 = "\n" + output13 + checkSum;
System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");
s = input.next();
}
}
else if (!s.toUpperCase().equals("QUIT")) {
System.out.println(s + " is invalid input.");
System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");
s = input.next();
}
}
System.out.println("The 10 digit ISBNs are \n" + output10);
System.out.println("The 13 digit ISBNs are \n" + output13);
}
}
Flowchart as a separate image since it's kinda small in the instructions doc
感谢您的帮助。
答案 0 :(得分:0)
是的,您在此for
循环中缺少增量器
for (int i=0; i <= s.length();) {
更改为
for (int i=0; i <= s.length(); i++) {
我确定您不希望<=
,也许只是<
如此
for (int i=0; i < s.length(); i++) {
顺便说一句,如果您调试代码,这很容易解决-这是一项基本技能-
修改
如果您具有以下代码(且s.length == 12)
for (int i=0; i < s.length(); i++) {
System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");
s = input.next();
}
然后它将执行12次。修正循环
答案 1 :(得分:-1)
更新代码,因为我在这里实施了一些建议:
package lab05a;
import java.util.Scanner;
public class Lab05A {
public static void main(String[] args) {
// Input for s
Scanner input = new Scanner(System.in); // Create new scanner
System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: "); // our ever-lasting prompt
String s = input.next(); // declare string variable "s" and set it equal to next input from user.
String output10 = ""; // Declaring string output10
String output13 = ""; // Declaring string output13
// main while loop
while (!"QUIT".equalsIgnoreCase(s)) { //this will run as long as the program does not receive an input of "QUIT", not case sensitive.
char checkDigit;
char checkSum = '0';
if (s.length() == 9) { //if the length of the inputted string is 9 characters...
int sum = 0; // initialize sum variable
for (int i=0; i < s.length(); i++) {
sum = sum + ((s.charAt(i) - '0') * (i + 1));
}
if (sum % 11 == 10) {
checkDigit = 'X';
}
else {
checkDigit = (char) ('0' + (sum % 11));
}
output10 = output10 + "\n" + s + checkDigit;
System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");
s = input.next();
}
else if (s.length() == 12) {
int sum = 0;
for (int i=0; i < s.length(); i++) {
if (i % 2 == 0) {
sum = sum + (s.charAt(i) - '0');
}
else {
sum = sum + (s.charAt(i) - '0') * 3;
}
checkSum = (char) (10 - sum % 10);
if (checkSum == 10) {
checkSum = 0;
}
output13 = "\n" + output13 + s + checkSum;
System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");
s = input.next();
}
}
else if (!"QUIT".equalsIgnoreCase(s)) {
System.out.println(s + " is invalid input.");
System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");
s = input.next();
}
}
System.out.println("The 10 digit ISBNs are \n" + output10);
System.out.println("The 13 digit ISBNs are \n" + output13);
}
}