简体   繁体   English

“这是我在这个 if 语句中多次发生的错误如何使这段代码正确执行

[英]"this is the error happened with me several times in this if statement how to make this code execute properly

using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine("please enter xhundred :");
        double xhundred = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("please enter xT :");
        double xT = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("please enter xzero :");
        double xzero = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("please enter t :");
        double T = Convert.ToDouble(Console.ReadLine());
        //this is the if statement of this peace of code
        if ((xhundred , xT , xzero ,T ) < 0 && (  xhundred, xT , xzero ,T ) > 100 ){
            Console.WriteLine("please enter a number from 0 to 100");
        }
        else {
            Console.WriteLine( "l" );
        }
        double t = ((xhundred - xT )/(xhundred - xzero ))*100;
        Console.WriteLine(t);
    }
}

at first i went to microsoft learn website and searching on the erroe code (cs0019) and i fond the code will not be running cause of the C# is not convertible to int and cs0019 is also generated when the subtraction operator - is applied to a string.起初我去微软学习网站并搜索错误代码 (cs0019),我觉得代码不会运行,因为 C# 不能转换为 int,当减法运算符 - 应用于字符串时也会生成 cs0019 . The addition operator + can be used with string but in my code there is no boolean i think the problem is the Operator '<' cannot be applied to operands of type '(double, double, double, double)' and 'int'加法运算符 + 可以与字符串一起使用,但在我的代码中没有布尔值我认为问题是运算符 '<' 不能应用于 '(double, double, double, double)' 和 'int' 类型的操作数

You can't compare a tuple and with a single int.您不能将元组和与单个 int 进行比较。 You have to do single comparisons for every parameter:您必须对每个参数进行单一比较:

if(xhundred < 0 || xT < 0 || xzero < 0 || T < 0 || xhundred > 100 || xT > 100 || xzero > 100 || T > 100)
{
  Console.WriteLine("please enter a number from 0 to 100");
}

If you want to compare all variables at once, you can also use LINQ, which is a bit more comfortable:如果你想一次比较所有变量,你也可以使用 LINQ,它更舒服一点:

int[] variables = new int[] { xhundred , xT , xzero ,T };
if(variables.Any(x => x < 0 || x > 100))
{
  Console.WriteLine("please enter a number from 0 to 100");
}

Or you do every check in a single if, so that the user knows which variable is out of value:或者您在单个 if 中进行每次检查,以便用户知道哪个变量超出了值:

Console.WriteLine("please enter xhundred :");
double xhundred = Convert.ToDouble(Console.ReadLine());
if(xhundred < 0 || xhundred > 100)
{
  Console.WriteLine("please enter a number from 0 to 100");
  return;
}
Console.WriteLine("please enter xT :");
double xT = Convert.ToDouble(Console.ReadLine());

if(xT< 0 || xT > 100)
{
  Console.WriteLine("please enter a number from 0 to 100");
  return;
}
// and so on

You can also capsulate that code in a method:您还可以将该代码封装在一个方法中:

private double? ReadValue(string name, int lowerBorder, int upperBorder)
{
    Console.WriteLine("please enter "+name+" :");
    double value = Convert.ToDouble(Console.ReadLine());
    if(value < lowerBorder || value> upperBorder)
    {
      Console.WriteLine("please enter a number from "+lowerBorder+" to "+upperBorder);
      return null;
    }
    return value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM