【问题标题】:Isbn generate the check digitisbn 生成校验位
【发布时间】:2010-09-02 19:54:27
【问题描述】:

此代码验证 ISBN 是否有效。对于九位输入,我想通过计算和附加校验位来形成一个有效的 ISBN。对于少于九位的输入,我希望它返回错误消息“请输入正确的数字”。我该怎么办?

   public class isbn
    {   //attributes
         private string isbnNum;
         //method   
         public string GetIsbn()
         {
             return this.isbnNum;
         }
           //constructor
           public isbn()
           {
               Console.Write("Enter Your ISBN Number: ");
               this.isbnNum = Console.ReadLine();

           }//end default constructor

            //method
           public string displayISBN()
           {

               return  this.GetIsbn();

           }


       public static void Main(string[] args)
        {
            //create a new instance of the ISBN/book class

            isbn myFavoriteBook = new isbn();

            //contains the method for checking validity 
            bool isValid = CheckDigit.CheckIsbn(myFavoriteBook.GetIsbn());

            //print out the results of the validity.
            Console.WriteLine(string.Format("Your book {0} a valid ISBN",
                                       isValid ? "has" : "doesn't have"));

            Console.ReadLine();

        }

public static class CheckDigit
{       // attributes
    public static string NormalizeIsbn(string isbn)
    {
        return isbn.Replace("-", "").Replace(" ", "");
    }
   public static bool CheckIsbn(string isbn) // formula to check ISBN's validity
    {
        if (isbn == null)
            return false;

        isbn = NormalizeIsbn (isbn);
        if (isbn.Length != 10)
            return false;

        int result;
        for (int i = 0; i < 9; i++)
            if (!int.TryParse(isbn[i].ToString(), out result))
                return false;

        int sum = 0;
        for (int i = 0; i < 9; i++)
            sum += (i + 1) * int.Parse(isbn[i].ToString());

        int remainder = sum % 11;
        if (remainder == 10)
            return isbn[9] == 'X';
        else
            return isbn[9] == (char)('0' + remainder);
    }

【问题讨论】:

  • 不,这是书本上的练习

标签: c# c#-3.0


【解决方案1】:

只需将其更改为附加最后一个字符,而不是检查它是否存在。以上内容可以稍微清理一下,但只需根据需要进行更改即可:

public static string MakeIsbn(string isbn) // string must have 9 digits
{
    if (isbn == null)
        throw new ArgumentNullException();

    isbn = NormalizeIsbn (isbn);
    if (isbn.Length != 9)
        throw new ArgumentException();

    int result;
    for (int i = 0; i != 9; i++)
        if (!int.TryParse(isbn[i].ToString(), out result))
            throw new ArgumentException()

    int sum = 0;
    for (int i = 0; i != 9; i++)
        sum += (i + 1) * int.Parse(isbn[i].ToString());

    int remainder = sum % 11;
    if (remainder == 10)
        return isbn + 'X';
    else
        return isbn + (char)('0' + remainder);
}

【讨论】:

  • 感谢您的帮助,我很感激,但程序必须能够同时做到这两点。因为最后一部分是在控制台顶部添加一个表单,并使其具有 2 个按钮,一个用于验证,一个用于生成。
  • @迈克尔。这就是为什么上面是一个具有新名称的单独方法,您可以将其添加到相关类中。可以进行重构以删除重复项,但首先要弄清楚为什么要先解决这两个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多