如何检查号码是在号码的左侧还是右侧

发布于 2025-01-17 05:19:36 字数 332 浏览 2 评论 0原文

如何检查数字是在数字的左侧还是右侧,

例如数字是2,我想知道它在12和23上的位置,

其中2是23的左侧,12的右侧,

我的代码

class Solution
{
   public static  void Main(String[] args)
   { 
      var number =  12;
      foreach(char c in number.ToString()){
         if(c=='2'){
            Console.WriteLine(c);
         }
      }
   }
}

How to check is a number is on left side or right side of number

for example number is 2 and i want where it is on 12 and 23

which is 2 is left side on 23 and right side on 12

my code

class Solution
{
   public static  void Main(String[] args)
   { 
      var number =  12;
      foreach(char c in number.ToString()){
         if(c=='2'){
            Console.WriteLine(c);
         }
      }
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情何以堪。 2025-01-24 05:19:36

检查一下这段代码:

CheckNumber(12);
CheckNumber(23);

static void CheckNumber(int number)
{
    var str = number.ToString();
    if (str[0] == '2')
        Console.WriteLine("2 is in left");
    if (str[^1] == '2')
        Console.WriteLine("2 is in right");
}

Check this code out:

CheckNumber(12);
CheckNumber(23);

static void CheckNumber(int number)
{
    var str = number.ToString();
    if (str[0] == '2')
        Console.WriteLine("2 is in left");
    if (str[^1] == '2')
        Console.WriteLine("2 is in right");
}
美人骨 2025-01-24 05:19:36
class Solution
{
   public static  void Main(String[] args)
   { 
      bool isLeft = true;
      var number =  12;
      foreach(char c in number.ToString()){
         if(c=='2'){
            if(isLeft)
            {
                Console.WriteLine("It's on the left.");
            }
            else 
            {
               Console.WriteLine("It's on the right");
            }
            Console.WriteLine(c);
         }
     isLeft = false;
      }
   }
}

编辑:
我添加了证明

class Program
    {
        static void Main(string[] args)
        {
            NewMethod(12);
            NewMethod(23);
        }

        private static void NewMethod(int number)
        {
            bool isLeft = true;
            foreach (char c in number.ToString())
            {
                if (c == '2')
                {
                    if (isLeft)
                    {
                        Console.WriteLine("It's on the left.");
                    }
                    else
                    {
                        Console.WriteLine("It's on the right");
                    }
                    Console.WriteLine(c);
                }
                isLeft = false;
            }
        }
    }

“控制台结果”

证明未编辑版本有效:

证明

class Solution
{
   public static  void Main(String[] args)
   { 
      bool isLeft = true;
      var number =  12;
      foreach(char c in number.ToString()){
         if(c=='2'){
            if(isLeft)
            {
                Console.WriteLine("It's on the left.");
            }
            else 
            {
               Console.WriteLine("It's on the right");
            }
            Console.WriteLine(c);
         }
     isLeft = false;
      }
   }
}

EDIT:
I added proof

class Program
    {
        static void Main(string[] args)
        {
            NewMethod(12);
            NewMethod(23);
        }

        private static void NewMethod(int number)
        {
            bool isLeft = true;
            foreach (char c in number.ToString())
            {
                if (c == '2')
                {
                    if (isLeft)
                    {
                        Console.WriteLine("It's on the left.");
                    }
                    else
                    {
                        Console.WriteLine("It's on the right");
                    }
                    Console.WriteLine(c);
                }
                isLeft = false;
            }
        }
    }

Console result

Proof un-edited version works:

Proof

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文