使用c#升序 [英] ascending order using c#

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

问题描述

大家好,



我对如何修改用C#编写的程序有一些疑问。

这个程序给出了以下输出按升序排列。



输出:

 1 2 3 4 5 6 7 8 9 10 





我的问题是:



我需要在输出中的第4个位置显示消息。



消息应该是这样的(你已达到第4个升序的位置)。



获得第4个位置后,代码将再次开始执行剩余的顺序,即第5,第6,第7,第8,第9和$ 10。





预期输出为:

 1 

2

3

(您已达到升序的第4位)

5

6

7

8

9

10





请在下面找到我的代码和t我应该改变它。请告诉我!建议非常明显。



我的代码是:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Text;

命名空间 ConsoleApplication1
{
class 计划
{
静态 void Main( string [] args)
{
int i = 0 ;
while (i< 10)
{


i = i + 1 ;
Console.WriteLine(i);
Console.ReadLine();


}
}
}
}





提前致谢!

解决方案

我认为代码如下所示。但是我想知道它是为了什么?

  for  int  i =  1 ; i <  =  10 ; i ++)
{
if (i == 4
{
Console.WriteLine( 您已达到升序的第4个位置为了);
}
else
{
Console.WriteLine(i);
}
}


这确实是一个非常复杂的问题,我想如果你使用那么那么 [ ^ ]您可以将其关闭。





类似于:



Console.WriteLine(i);

  if (i ==  4 
打印( @ (您已达到升序的第4位));
else
Console.WriteLine(i);





祝你好运,

Edo


将您的代码更改为:





  int  i =  0 ; 
int n = 4 ;
while (i< 10)
{

i = i + 1 ;
if (i = n)
Console.WriteLine( 您已达到递增顺序的{0} {1}位置,n,GetEnding(n));
else
Console.WriteLine(i);

Console.ReadLine();
}





然后添加方法:



< pre lang =cs> private string GetEnding( int n)
{
var ones = n% 10 ;
switch (ones)
case 1
return ST;
case 2
return nd;
case 3
return rd;
默认
return th;

}





这应该做你想要的,如果你改变了它也应该这样做继续工作。 (我相信)。



可以在这个主题中找到GetEnding方法的替代方案:



在.NET中有一种简单的方法可以获得数字的st,nd,rd和th结尾吗? [ ^ ]


Hi all,

I have a few doubts in how to modify my program written in C#.
This program gives below output in ascending order.

The output:

1 2 3 4 5 6 7 8 9 10



My question is :

I need to display the message when the 4th position in the output is reached.

The message should be like this ("You have reached 4th position of ascending order").

After getting the 4 th postions the code again will start execute the remaining order, which means 5th,6th,7th,8th,9th and 10th.


The expected output is:

1

2

3

(" you have been reached 4th position of ascending order")

5

6

7

8

9

10



Please find my code below and tell me were I should alter it. Please let me know! Advice is very appreciable.

My code is:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
         while(i<10)
            {
             
                
                i = i + 1;
                Console.WriteLine(i);
                Console.ReadLine();

               
            }
        }
    }
}



Thanks in advance!

解决方案

I think the code could be as below. But I'm wondering what's it for?

for (int i = 1; i <= 10; i++)
{
    if (i == 4)
    {
        Console.WriteLine(" you have been reached 4th position of ascending order");
    } 
    else
    {
        Console.WriteLine(i);
    }
}


This is indeed a very complex question, I would imagine that perhaps if you'd use IF THEN ELSE[^] you just might be able to pull this off.


Something like:

Console.WriteLine(i);

if (i==4)
   print(@"(you have been reached 4th position of ascending order)");
else
  Console.WriteLine(i);



Good luck,
Edo


Change you code to this:


int i = 0;
int n = 4;
while(i<10)
 {

      i = i + 1;
      if(i = n)
         Console.WriteLine("You have reached the {0}{1} position of ascending order", n, GetEnding(n));
      else
      Console.WriteLine(i);

      Console.ReadLine();
}



Then add the method:

private string GetEnding(int n)
{
   var ones = n % 10;
   switch(ones)
   case 1:
      return "st";
   case 2:
     return "nd";
   case 3:
     return "rd";
   default:
       return "th";
    
}



That should do what you want, it should also make it that if you change n it continues to work. (i believe).

An alternative to the GetEnding method can be found in this thread:

Is there an easy way in .NET to get "st", "nd", "rd" and "th" endings for numbers?[^]


这篇关于使用c#升序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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