0

因此,作为我班级的一个项目,我需要制作一个程序,让用户输入 ISBN 号码的前 n 位数字并以第十位数字进行响应。我一直在无休止地寻找可以让我的程序询问用户是否要再次运行该程序,然后如果他们输入“是”或“Y”则让它再次运行该程序的东西。我发现的东西没有用,通常只是循环“你想输入另一个 ISBN”,而没有真正让他们输入 ISBN。我的代码在下面,谢谢大家的帮助!

import java.util.Scanner;
public class ISBNCheckSum {

    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System. in );
        // limiter 
        int isbn10 = 9;
        // to take in response of user
        long userResponse;
        // accumulator 
        int ISBnNum = 1;
        //current count of ISBN
        long isbnCount = 0;
        // This is used to multiply the userresponse by 1,2,3... up to 9
        int multiplier = 1;

        while (ISBnNum <= isbn10) 
        {
            System.out.println("Please ISBN number  " + ISBnNum);
            //to enter the User response
            userResponse = keyboard.nextInt();
            //Multiply the user response by multiplier variable
            userResponse = userResponse * multiplier;
            //add to accumulator 
            ISBnNum = ISBnNum + 1;
            // put user into final answer
            isbnCount = isbnCount + userResponse;
            // increase multiplier
            multiplier = multiplier + 1;
        }

        long checkSum;
        checkSum = isbnCount % 11;
        System.out.println(checkSum);
    }
}
}
4

1 回答 1

1

尝试这样的事情(我已经更改了 iSBnNum 变量名):

    boolean askAgain = true;
    while (askAgain) {
        iSBnNum = 1;
        while (iSBnNum <= isbn10) {
            System.out.println("Please ISBN number  " + iSBnNum);
            // to enter the User response
            userResponse = keyboard.nextInt();
            // Multiply the user response by multiplier variable
            userResponse = userResponse * multiplier;
            // add to accumulator
            iSBnNum = iSBnNum + 1;
            // put user into final answer
            isbnCount = isbnCount + userResponse;
            // increase multiplier
            multiplier = multiplier + 1;
        }

        System.out.println("Ask again (Y/N)?");
        String answer = keyboard.next();

        askAgain = answer.equalsIgnoreCase("Y");
    }
于 2015-02-18T15:43:07.603 回答