猿问

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';

                }

http://img1.mukewang.com/618cb2ae000119dd16010848.jpg

    

幕布斯7119047
浏览 245回答 2
2回答

紫衣仙女

是的,您在此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++) {&nbsp; &nbsp; System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");&nbsp; &nbsp; s = input.next();}然后它将执行 12 次。修复你的循环

喵喵时光机

更新代码,因为我在这里实施了一些建议:package lab05a;import java.util.Scanner;public class Lab05A {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // Input for s&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in); // Create new scanner&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: "); // our ever-lasting prompt&nbsp; &nbsp; &nbsp; &nbsp; String s = input.next(); // declare string variable "s" and set it equal to next input from user.&nbsp; &nbsp; &nbsp; &nbsp; String output10 = ""; // Declaring string output10&nbsp; &nbsp; &nbsp; &nbsp; String output13 = ""; // Declaring string output13&nbsp; &nbsp; &nbsp; &nbsp; // main while loop&nbsp; &nbsp; &nbsp; &nbsp; while (!"QUIT".equalsIgnoreCase(s)) { //this will run as long as the program does not receive an input of "QUIT", not case sensitive.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char checkDigit;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char checkSum = '0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (s.length() == 9) { //if the length of the inputted string is 9 characters...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int sum = 0; // initialize sum variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i < s.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + ((s.charAt(i) - '0') * (i + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sum % 11 == 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkDigit = 'X';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkDigit = (char) ('0' + (sum % 11));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output10 = output10 + "\n" + s + checkDigit;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = input.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if&nbsp; (s.length() == 12) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i < s.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + (s.charAt(i) - '0');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + (s.charAt(i) - '0') * 3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkSum = (char) (10 - sum % 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (checkSum == 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkSum = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output13 = "\n" + output13 + s + checkSum;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = input.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (!"QUIT".equalsIgnoreCase(s)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(s + " is invalid input.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the first 9 or 12 digits of an ISBN number. Enter QUIT to exit: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = input.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The 10 digit ISBNs are \n" + output10);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The 13 digit ISBNs are \n" + output13);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答