功能问题。 [英] Problem with a function.

查看:98
本文介绍了功能问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图让这个函数从字符串中返回一个整齐的表。



这是我期望得到的。



3.

1415926535 8979323846 2643383279 5028841971 6939937510

5820974944 5923078164 0628620899 8628034825 3421170679

8214808651 3282306647 0938446095 5058223172 5359408128

4811174502 8410270193 8521105559 6446229489 5493038196

4428810975 6659334461 2847564823 3786783165 2712019091

4564856692 3460348610 4543266482 1339360726 0249141273

7245870066 0631558817 4881520920 9628292540 9171536436

7892590360 0113305305 4882046652 1384146951 9415116094

3305727036 5759591953 0921861173 8193261179 3105118548

0744623799 6274956735 1885752724 8912279381 8301194912

9833673362 4406566430 8602139494 6395224737 1907021798

6094370277 0539217176 2931767523 8467481846 7669405132

0005681271 4526356082 7785771342 7577896091 7363717872

1468440901 2249534301 4654958537 1050792279 6892589235

4201995611 2129021960 8640344181 5981362977 4771309960

5187072113 4999999837 2978049951 0597317328 1609631859

5024459455 3469083026 4252230825 3344685035 2619311881

7101000313 7838752886 5875332083 8142061717 7669147303

5982534904 2875546873 1159562863 8823537875 9375195778

1857780532 1712268066 1300192787 6611195909 2164201989




然而,这是我实际得到的。



3。

1415926 535897932 384626433 832795028 841971693

9937510 582097494 459230781 640628620 899862803

4825342 117067982 148086513 282306647 093844609

5505822 317253594 081284811 174502841 027019385

2110555 964462294 895493038 196442881 097566593

3446128 475648233 786783165 271201909 145648566

9234603 486104543 266482133 936072602 491412737

2458700 660631558 817488152 092096282 925409171

5364367 892590360 011330530 548820466 521384146

9519415 116094330 572703657 595919530 921861173

8193261 179310511 854807446 237996274 956735188

5752724 891227938 183011949 129833673 362440656

6430860 213949463 952247371 907021798 609437027

7053921 717629317 675238467 481846766 940513200

0568127 145263560 827785771 342757789 609173637

1787214 684409012 249534301 465495853 710507922

7968925 892354201 995611212 902196086 403441815

9813629 774771309 960518707 211349999 998372978

0499510 597317328 160963185 950244594 553469083

0264252 230825334 468503526 193118817 101000313

7838752 886587533 208381420 617177669 147303598

2534904 287554687 311595628 638823537 875937519

5778185 778053217 122680661 300192787 661119590

9216420 1989




所以,我的问题是为什么会发生这种情况?我怎么可能修复它?



代码:

  public   string  PrintAsTables()
{
string reg = this .Print();
reg = reg.Insert( 2 ,Environment.NewLine);
int times = 0 ;
for int x = 2 ; x < = reg.Length - 1 ; x = x + 10
{
if (times == 5
{
reg = reg.Insert(x,Environment.NewLine);
次= 0 ;
}
次+ = 1 ;
reg = reg.Insert(x, );

}
return reg;
}

解决方案

  public   static   string  PrintAsTables()
{
string reg = Pi;
reg = reg.Insert( 2 ,Environment.NewLine);
int times = 0 ;
// 14因为你有newLine所以第一个空格可以在第14个位置
for int x = 14 ; x < = reg.Length - 1 ; x = x + 10
{
次+ = 1 ;
if (times == 5
{
reg = reg.Insert(x,Environment.NewLine);
x = x + 2 ;
次= 0 ;
}
else
{
reg = reg.Insert(x, );
}
}
return reg;
}





这完全是关于 Environment.NewLine 它的定义是环境,但在Windows中是2个特殊字符\\\\ n所以如果你插入字符串 Environment.NewLine 显示的下一个索引也应该递增。


我可能会通过使用StringBuilder开始,以及几个简单的计数器:

<预LANG = CS> <跨度类= 代码关键字>串 INP = <跨度类= 代码串> <跨度类= 代码串> 1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480 744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989\" ;
StringBuilder sb = new StringBuilder();
int groupSize = 10 ;
int groupsPerLine = 5 ;
int inGroup = 0 ;
int inLine = 0 ;
foreach char c in inp)
{
sb.Append(c);
inGroup ++;
if (inGroup == groupSize)
{
sb.Append(' ');
inGroup = 0 ;
inLine ++;
if (inLine == groupsPerLine)
{
sb.AppendLine();
inLine = 0 ;
}
}
}
return sb.ToString();


So I'm trying to make this function return a neat table out of a string.

This is what I expect to get.

3.
1415926535 8979323846 2643383279 5028841971 6939937510
5820974944 5923078164 0628620899 8628034825 3421170679
8214808651 3282306647 0938446095 5058223172 5359408128
4811174502 8410270193 8521105559 6446229489 5493038196
4428810975 6659334461 2847564823 3786783165 2712019091
4564856692 3460348610 4543266482 1339360726 0249141273
7245870066 0631558817 4881520920 9628292540 9171536436
7892590360 0113305305 4882046652 1384146951 9415116094
3305727036 5759591953 0921861173 8193261179 3105118548
0744623799 6274956735 1885752724 8912279381 8301194912
9833673362 4406566430 8602139494 6395224737 1907021798
6094370277 0539217176 2931767523 8467481846 7669405132
0005681271 4526356082 7785771342 7577896091 7363717872
1468440901 2249534301 4654958537 1050792279 6892589235
4201995611 2129021960 8640344181 5981362977 4771309960
5187072113 4999999837 2978049951 0597317328 1609631859
5024459455 3469083026 4252230825 3344685035 2619311881
7101000313 7838752886 5875332083 8142061717 7669147303
5982534904 2875546873 1159562863 8823537875 9375195778
1857780532 1712268066 1300192787 6611195909 2164201989


However, this is what I actually get.

3.
1415926 535897932 384626433 832795028 841971693
9937510 582097494 459230781 640628620 899862803
4825342 117067982 148086513 282306647 093844609
5505822 317253594 081284811 174502841 027019385
2110555 964462294 895493038 196442881 097566593
3446128 475648233 786783165 271201909 145648566
9234603 486104543 266482133 936072602 491412737
2458700 660631558 817488152 092096282 925409171
5364367 892590360 011330530 548820466 521384146
9519415 116094330 572703657 595919530 921861173
8193261 179310511 854807446 237996274 956735188
5752724 891227938 183011949 129833673 362440656
6430860 213949463 952247371 907021798 609437027
7053921 717629317 675238467 481846766 940513200
0568127 145263560 827785771 342757789 609173637
1787214 684409012 249534301 465495853 710507922
7968925 892354201 995611212 902196086 403441815
9813629 774771309 960518707 211349999 998372978
0499510 597317328 160963185 950244594 553469083
0264252 230825334 468503526 193118817 101000313
7838752 886587533 208381420 617177669 147303598
2534904 287554687 311595628 638823537 875937519
5778185 778053217 122680661 300192787 661119590
9216420 1989


So, my question is why is this happening? and how could I possibly fix it?

Code:

public string PrintAsTables()
{
    string reg = this.Print();
    reg = reg.Insert(2, Environment.NewLine);
    int times = 0;
    for (int x = 2; x <= reg.Length - 1; x = x + 10)
    {
        if (times == 5)
        {
            reg = reg.Insert(x, Environment.NewLine);
            times = 0;
        }
        times += 1;
         reg = reg.Insert(x, " ");

    }
    return reg;
}

解决方案

public static string PrintAsTables()
{
    string reg = Pi;
    reg = reg.Insert(2, Environment.NewLine);
    int times = 0;
    //14 because u had newLine so first space can be on 14th places
    for (int x = 14; x <= reg.Length - 1; x = x + 10)
    {
        times += 1;
        if (times == 5)
        {
            reg = reg.Insert(x, Environment.NewLine);
            x = x + 2;
            times = 0;
        }
        else
        {
            reg = reg.Insert(x, " ");
        }
    }
    return reg;
}



It's all about Environment.NewLine its definied by Environment but in windows is 2 special chars "\r\n" so if u inserting into string Environment.NewLine next index to display should be incremented too.


I would probably do it by using a StringBuilder to start with, and a couple of simple counters:

string inp = "1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989";
StringBuilder sb = new StringBuilder();
int groupSize = 10;
int groupsPerLine = 5;
int inGroup = 0;
int inLine = 0;
foreach (char c in inp)
    {
    sb.Append(c);
    inGroup++;
    if (inGroup == groupSize)
        {
        sb.Append(' ');
        inGroup = 0;
        inLine++;
        if (inLine == groupsPerLine)
            {
            sb.AppendLine();
            inLine = 0;
            }
        }
    }
return sb.ToString();


这篇关于功能问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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