所以我和我的朋友收到了实现此功能的编程任务:

所以你必须输入x和输出y.原样,这很容易,但事情就是这样:我们可以在没有变量的情况下,只使用一个Console.ReadLine()实例(并将其解析为double),然后将结果传递给多个"if"检查吗?使用新的类和方法也是作弊:我们必须在main()中做所有事情.
以下是带有一个变量的程序示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4 {
class Program {
static void Main(string[] args) {
double x;
x = double.Parse(Console.ReadLine());
if (x < -2.0) { Console.Write(2.0); }
if ((x < -1.0)&&(x>=-2.0)) { Console.Write(1.0); }
if ((x < 0.0)&&(x>=-1)) { Console.Write(0.0); }
if ((x>=0.0)&&(x<1.0)) { Console.Write(x); }
if (x >= 1.0) { Console.Write(1.0); }
}
}
}
Run Code Online (Sandbox Code Playgroud)