C# 方法,该方法将实例变量的值复制为反向数字&;在C中交换数字#
我被我的C#程序卡住了,就是不知道如何使它工作。 首先,我应该为4digit数字创建一个实例变量,并为该实例变量创建getter和setter方法(使用该方法完成) 其次,我应该创建4种方法:C# 方法,该方法将实例变量的值复制为反向数字&;在C中交换数字#,c#,C#,我被我的C#程序卡住了,就是不知道如何使它工作。 首先,我应该为4digit数字创建一个实例变量,并为该实例变量创建getter和setter方法(使用该方法完成) 其次,我应该创建4种方法: 数字求和的方法 方法复制实例变量的值并反转数字 复制实例变量值并交换第一位和最后一位数字以及第四位或第二位和第三位数字的方法 最后应该有一个Main方法Main方法,它创建一个对象,为实例变量指定一个四位数的数字,并测试所有方法。 现在我已经找到了下面的代码,但是所有的方法都返回0,除了一个工作正常的方法
现在我已经找到了下面的代码,但是所有的方法都返回0,除了一个工作正常的方法。
如果有人能帮我修改这个代码,我将不胜感激
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 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;
}
//Main方法,该方法创建一个对象,为实例变量指定一个四位数字,并测试所有方法
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%正确。干得好。我只需要做一些小的改动就可以让一切正常工作 对于
sumdights()
和RevNum()
,您直接使用了number
。问题是,当您执行number=number/10
时,您正在用新的数字覆盖用户输入的数字。在完成Sum()
之后,number
最终变成了0。。。因此,当其他3个方法执行时,它们正试图对数字0执行操作
为了解决这个问题,我在这两种方法中将number
分配给一个临时变量
对于SecondThirdDigit()
和FirstLastDigit()
方法,您的公式也是正确的。但是,您将firstlast
分配给一个4位数字,然后在firstlast.length>1
时执行while循环。因为长度是4,而你的循环从来没有改变过这个,所以这变成了一个永无止境的循环。我不知道你为什么认为你需要一个循环
无论如何,为了解决这个问题,我删除了循环,您的代码现在可以工作了。另外,您将结果直接写入控制台,而不是将其分配给firstlast
和secondthird
变量,因此我也修复了这些变量
除此之外,代码中的所有内容都是不变的。以下是一个更新版本,现在应该可以正常工作:
//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;
}
//计算并返回实例变量位数总和的方法
公共整数
{
//分配给临时变量的编号
int TEMPNAME=数字;
对于(int i=0;i<5;i++)
{
r=临时编号%10;
tempNumber=tempNumber/10;
sum=sum+r;
}
回报金额;
}
//方法复制实例变量的值,然后按相反顺序返回副本的值
public int RevNum()
{
//分配给临时变量的编号
int TEMPNAME=数字;
而(tempNumber>0)
{
反向=反向*10+(临时编号-(临时编号/10)*10);
tempNumber=tempNumber/10;
}
反向返回;
}
//方法复制实例变量的值&交换数字的第一位和最后一位
公共字符串FirstLastDigit()
{
firstlast=number.ToString();
char[]digits=firstlast.ToCharArray();
char firstDigit=数字[0];
数字[0]=数字[digits.Length-1];
数字[digits.Length-1]=第一位数字;
//将结果分配给firstlast而不是控制台输出
firstlast=新字符串(数字);
首先返回,最后返回;
}
//方法,该方法复制实例变量的值并交换数字的第二位和第三位
公共字符串secondThirdGit()
{
secondthird=number.ToString();
char[]digits=second-third.ToCharArray();
char firstDigit=数字[1];
数字[1]=数字[digits.Length-2];
数字[digits.Length-2]=第一位数字;
//将结果分配给secondthird,而不是控制台输出
secondthird=新字符串(数字);
第二次返回第三次;
}
您的问题是什么?在每种方法中,您都会覆盖根编号。。在更改之前创建此编号的副本代码的哪部分不起作用?出现了什么错误?RevNum()、SecondThirdDigit和FirstLastDigit方法没有提供正确的输出。。他们只是给了0..耶,现在开始工作了。非常感谢您不仅修复了我的代码,还提供了详细的解释。这真的很有帮助。再次非常感谢。
//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;
}