首页 > 解决方案 > Java ISBN 校验和生成器——无限循环?

问题描述

我需要使用字符串、字符和一堆嵌套循环和条件语句为我的 CS 类构建这个 ISBN 校验和生成器(适用于 ISBN-10 和 ISBN-13)。在这乱七八糟的某个地方,我认为某些东西正在触发无限循环,因为当我被提示输入时,我输入输入并按回车,它只是进入一个新行并希望我输入更多数据,我猜是什么时候它应该在每次成功输入后再次提示我输入另一个,否则告诉我它不正确,然后仍然再次要求另一个输入。当我输入 quit 时,它不会结束程序并像预期的那样显示校验和结果,而是表现出与其他输入相同的行为。

到目前为止我的代码:

/******************************************************************************
 * 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);
    }
}

指示

流程图作为单独的图像,因为它在说明文档中有点小

谢谢你的帮助。

标签: javaloopswhile-loopintegerinfinite-loop

解决方案


是的,您在此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 次。修复你的循环


推荐阅读