繁体   English   中英

将实例变量的值复制到C#中数字的反向数字和交换数字的方法

[英]Methods that copies the value of the instance variable to reverse digits & swap digits of a number in C#

我陷入了我的C#程序,只是不知道如何使它工作。 首先,我应该为4位数字创建一个实例变量,并为该实例变量创建getter和setter方法(完成该操作)。

其次,我应该创建4种方法:

  1. 数字求和的方法。
  2. 复制实例变量的值并反转数字的方法。
  3. 复制实例变量的值并交换第一和最后一位和第四位或交换第二和第三位的方法。

最后,应该有一个Main方法Main方法创建一个对象,为实例变量分配一个四位数的数字,并测试所有方法。
现在我想出了以下代码,但是所有方法都返回0,但SumDigits可以正常工作。
如果有人能帮忙解决此代码,我将不胜感激。

    public class myClass
    {

    public int number=0;//instance variable
    public int r;
    public int sum = 0;
    public int rem ;
    public int reverse = 0;
    public string firstlast;
    public string secondthird;

    public myClass()//Constructor
    {
    }
        // set & get property
        public int MyNumber
        {
            get
            {
                return number;
            }
            set
            {
                number=value;
            }
        }

        //Setter and getter methods
    public void SetNumber(int fourNumber)
     {
        number = fourNumber;
     }
    public int GetNumber()
   {
       return number;
   }

//计算并返回实例变量的位数之和的方法

        public int SumDigits()
   {

           for (int i = 0; i < 5; i++)
           {
                   r = number % 10;
                   number = number / 10;
                   sum = sum + r;
           }

       return sum;
   }

//复制实例变量值的方法,然后以相反的顺序返回副本的值

        public int RevNum()
    {

        while (number> 0)
        {
            reverse = reverse * 10 + (number - (number / 10) * 10);
            number = number / 10;

        }
        return reverse;
    }

//复制实例变量的值并交换数字的第一位和最后一位的方法

        public string FirstLastDigit()
       {
           firstlast = number.ToString();
            while (firstlast.Length > 1)
            {
               {
                   char[] digits = firstlast.ToCharArray();
                   char firstDigit = digits[0];
                   digits[0] = digits[digits.Length - 1];
                   digits[digits.Length - 1] = firstDigit;
                   Console.WriteLine(new string(digits));
               }
           }
           return firstlast;
       }

//复制实例变量值并交换数字的第二和第三位的方法

        public string SecondThirdDigit()
        {
            secondthird = number.ToString();
            while (firstlast.Length > 1)
            {
                    char[] digits = secondthird.ToCharArray();
                    char firstDigit = digits[1];
                    digits[1] = digits[digits.Length - 2];
                    digits[digits.Length - 2] = firstDigit;
                    Console.WriteLine(new string(digits));


            }
            return secondthird;
        }

//创建对象的主方法,为实例变量分配一个四位数的数字,并测试所有方法

       public static void Main(string[] args)
       {
           myClass myNum = new myClass();

           myNum.GetNumber();

           Console.WriteLine("ENTER THE NUMBER");
           myNum.number = Convert.ToInt32(Console.ReadLine());
           Console.WriteLine("\nNumber is " + myNum.number);
           Console.ReadLine();


           myNum.SumDigits();
           Console.WriteLine("\nSum is " + myNum.sum);

           myNum.RevNum();
           Console.WriteLine("\nThe reverse of number " + myNum.reverse);

           myNum.FirstLastDigit();
           Console.WriteLine("\nThe swap of first and last digit is" + myNum.firstlast);

           myNum.SecondThirdDigit();
           Console.WriteLine("\nExchange of Second and third digit is" + myNum.secondthird);

           Console.ReadLine();
       }
    }
}

我快速查看了一下,您的代码正确率为99%。 那里的工作很好。 我只需要进行一些小的更改就可以使所有功能正常运行。

对于SumDigits()RevNum() ,您直接使用number 问题是当您输入number = number / 10 ,您正在用新的数字覆盖用户输入的数字。 最终,在Sum()完成后, number变为0 ...因此,当其他3个方法执行时,它们正试图对数字0进行操作。

为了解决这个问题,我在这两种方法中将number分配给了临时变量。

对于SecondThirdDigit()FirstLastDigit()方法,您的公式也是正确的。 但是,您为firstlast分配了一个4位数字,然后在firstlast.length > 1执行了while循环。 因为长度为4,并且您的循环从未更改过,所以这变成了永无止境的循环。 我不确定您为什么认为需要在此处循环?

无论如何解决此问题,我都删除了循环,您的代码现在可以正常工作了。 同样,您将结果直接写入控制台,而不是将其分配给firstlastsecondthird变量,因此我也修复了这些问题。

除此之外,您的代码中的所有其他内容都保持不变。 这是适合您的更新版本,现在应该可以正常使用:

//method to calculate and return the sum of the digits of the instance variable
public int SumDigits()
{
    // Assigned number to temp variable
    int tempNumber = number;

    for (int i = 0; i < 5; i++)
    {
        r = tempNumber % 10;
        tempNumber = tempNumber / 10;
        sum = sum + r;
    }

    return sum;
}

//method that copies the value of the instance variable, and then returns the value of the copy in a reverse order
public int RevNum()
{
    // Assigned number to temp variable
    int tempNumber = number;

    while (tempNumber > 0)
    {
        reverse = reverse * 10 + (tempNumber - (tempNumber / 10) * 10);
        tempNumber = tempNumber / 10;
    }
    return reverse;
}

//method that copies the value of the instance variable & swap the first and last digit of the number
public string FirstLastDigit()
{
    firstlast = number.ToString();

    char[] digits = firstlast.ToCharArray();
    char firstDigit = digits[0];
    digits[0] = digits[digits.Length - 1];
    digits[digits.Length - 1] = firstDigit;
    // Assigned result to firstlast instead of console output
    firstlast = new string(digits);  

    return firstlast;
}

//method that copies the value of the instance variable & swaps the second and third digit of the number
public string SecondThirdDigit()
{
    secondthird = number.ToString();

    char[] digits = secondthird.ToCharArray();
    char firstDigit = digits[1];
    digits[1] = digits[digits.Length - 2];
    digits[digits.Length - 2] = firstDigit;
    // Assigned result to secondthird instead of console output
    secondthird = new string(digits);

    return secondthird;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM