编写程序,求函数x<0时y=-1;x=0时y=0;x>0时y=1 的值

解决方案 »

  1.   

    这个不难吧?有什么特别的要求吗?if(x<0)
    {
      y=-1;
    }
    else if(x=0)
    {
      y=0;
    }
    else
    {
      y=1;
    }
      

  2.   

    正解啊.
    可以再加一个输入 X的值
    还有一个输出 y的值.
    Console.WriteLine("来个数:");
            string x=Console.ReadLine();
    Console.WriteLine("y的值为{0}:",x);
      

  3.   


            static void Main(string[] args)
            {
                Console.Write("请输入X的值:");
                string data = Console.ReadLine();
                bool flag = IsNumber(data);
                if (flag == true)
                {
                    int intData = Convert.ToInt32(data);
                    int Y;
                    if (intData > 0)
                    {
                        Y = 1;
                    }
                    else
                    {
                        if (intData == 0)
                        {
                            Y = 0;
                        }
                        else
                        {
                            Y = -1;
                        }
                    }
                    Console.WriteLine("根据您输入的X值,得出Y的值为:"+Y.ToString());
                }
                else
                {
                    Console.WriteLine("请输入整数");
                }
                Console.ReadKey();
            }        static bool IsNumber(string data)
            {
                Regex r = new Regex("^[-]{0,1}\\d+$");//这个正则只验证整数,根据需要,自己写正则
                Match m = r.Match(data);
                if (m.Success)
                {
                    return true;
                }
                return false;
            }
      

  4.   

            static void Main(string[] args)
            {
                loop1:
                //编写程序,求函数x <0时y=-1;x=0时y=0;x>0时y=1 的值
                int i = int.Parse(Console.ReadLine());
                try
                {
                    Console.WriteLine(i / Math.Abs(i));
                }
                catch
                {
                    Console.WriteLine("0");
                }
                goto loop1;
            }脑子进水的写法。。为我的想象力鼓掌吧!
      

  5.   

        class Program
        {
            static void Main(string[] args)
            {
                loop1:
                //编写程序,求函数x <0时y=-1;x=0时y=0;x>0时y=1 的值
                int i = int.Parse(Console.ReadLine());
                Console.WriteLine(i == 0 ? 0 : i / Math.Abs(i));
                goto loop1;
            }
        }OK,进水完成。
      

  6.   

        class Program 
        { 
            static void Main(string[] args) 
            { 
                loop1: 
                //编写程序,求函数x <0时y=-1;x=0时y=0;x>0时y=1 的值 
                int i = int.Parse(Console.ReadLine()); 
                Console.WriteLine(i == 0 ? 0 : i / Math.Abs(i));             goto loop1; 
            } 
        } OK,进水完成。只用了一行代码。