如何使用c#交换四位数的数字? [英] How to swap numbers of four digits numbers using c#?

查看:126
本文介绍了如何使用c#交换四位数的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想交换输入用户的四位数字。我写了那些代码

  private   void  btnSwap_Click( object  sender,EventArgs e)
{
int number,digit1,digit2,digit3,digit4;
number = Convert.ToInt32(txtNumber.Text);
digit1 = number% 10 ;
digit2 =(数字/ 10 )% 10 ;
digit3 =(数字/ 100 )% 10 ;
digit4 =(数字/ 1000 )% 10 ;
digit4 = digit1;
digit1 = digit3;
digit3 = digit2;
digit2 = digit1;
lbReserved.Text = number.ToString();

}



但是这些代码返回相同的数字?怎么做?

解决方案

参考:反转字符串的最佳方法 [ ^ ]

 lbReserved.Text =  new  < span class =code-sdkkeyword> String (Array.Reverse(txtNumber.Text.ToCharArray())); 


当然,最好的答案是DamithSL(五位顺便说一下)。但我认为你应该理解你在代码中做了什么。



第一部分是OK:



  int  number,digit1,digit2,digit3,digit4; 
number = Convert.ToInt32(txtNumber.Text);
digit1 = number% 10 ;
digit2 =(数字/ 10 )% 10 ;
digit3 =(数字/ 100 )% 10 ;
digit4 =(数字/ 1000 )% 10 ;





如果你刚刚调试了这个,你会发现在此之后你已经有了相反的数字(如果数字是1234,那你就得到了这个数字) 4位数字1,3位数字2 ...)

所以简单地添加:

 lblReserved.Text =  string  .Concat(digit1,digit2,digit3,digit4); 



会给你正确答案。



而不是那样,你开始按照特定顺序分配值,你必须明白,在给变量赋值后,前一个值就会丢失。这是胡说八道。



最后,你给lbReserved原始值(lbReserved.Text = number.ToString()),所以当然你看到的是原版价值。



很抱歉这个答案很长,但是看到代码看起来你是一个新手并且有一些错误的概念值得一解。 :)


因为你在赋值中使用了已经改变的值。

交换两个数字的正确逻辑,比如说 d1 d2 是:

  int  tmp; 
tmp = d1;
d1 = d2;
d2 = tmp; // 这会将d1的旧(正确)值分配给d2


I want to swap numbers of four digits number that enter users. And I wrote that codes

private void btnSwap_Click(object sender, EventArgs e)
       {
           int number, digit1, digit2, digit3, digit4;
           number = Convert.ToInt32(txtNumber.Text);
           digit1 = number % 10;
           digit2 = (number / 10) % 10;
           digit3 = (number / 100) % 10;
           digit4 = (number / 1000) % 10;
           digit4 = digit1;
           digit1 = digit3;
           digit3 = digit2;
           digit2 = digit1;
           lbReserved.Text = number.ToString();

       }


But that codes return same number ? How to do it?

解决方案

refer : Best way to reverse a string[^]

lbReserved.Text = new String(Array.Reverse(txtNumber.Text.ToCharArray()));


Of course,the best answer is DamithSL's (fived btw). But I think you should understand what you are doing in your code.

The first part is OK:

int number, digit1, digit2, digit3, digit4;
number = Convert.ToInt32(txtNumber.Text);
digit1 = number % 10;
digit2 = (number / 10) % 10;
digit3 = (number / 100) % 10;
digit4 = (number / 1000) % 10;



If you just had debugged this, you'd see that after this,you already have the numbers in reverse order (if number is 1234, you've got after this 4 in digit1,3 in digit 2...)
So simply adding:

lblReserved.Text=string.Concat(digit1,digit2,digit3,digit4);


would have given you the right answer.

Instead of that, you start assigning the values in no specific order, and you must understand that after you give a variable a value,the previous one is lost. It's a nonsense.

Finally, you give the lbReserved the original value (lbReserved.Text=number.ToString()), so of course what you see is the original value.

Sorry for the long answer,but seeing the code it seems you are a newbie and have some wrong concepts that deserved an explanation. :)


Because you are using, in assignments, already changed values.
The correct logic to swap two numbers, say d1 and d2 is:

int tmp;
tmp = d1;
d1 = d2;
d2 = tmp; // this assigns the old (correct) value of d1 to d2


这篇关于如何使用c#交换四位数的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆