第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java ISBN 校驗和生成器——無限循環(huán)?

Java ISBN 校驗和生成器——無限循環(huán)?

幕布斯7119047 2021-11-11 14:04:37
我需要使用字符串、字符和一堆嵌套循環(huán)和條件語句為我的 CS 類構建這個 ISBN 校驗和生成器(用于 ISBN-10 和 ISBN-13)。在這個爛攤子的某個地方,我認為有些東西正在觸發(fā)無限循環(huán),因為當我被提示輸入時,我給出輸入并按回車鍵,它只是轉到一個新行并希望我輸入更多數據,我猜是什么時候它應該在每次成功輸入后再次提示我輸入另一個,否則告訴我這是不正確的,然后仍然再次要求輸入另一個。當我輸入 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';                }    
查看完整描述

2 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

是的,您在此for循環(huán)中缺少增量器


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();

}

然后它將執(zhí)行 12 次。修復你的循環(huán)


查看完整回答
反對 回復 2021-11-11
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

更新代碼,因為我在這里實施了一些建議:


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

    }

}


查看完整回答
反對 回復 2021-11-11
  • 2 回答
  • 0 關注
  • 266 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號