C# 同一变量具有两个不同值的If条件
我必须允许上传两种不同类型的图像尺寸 图像的宽度可以是370或602 如何使用if-image-widthC# 同一变量具有两个不同值的If条件,c#,C#,我必须允许上传两种不同类型的图像尺寸 图像的宽度可以是370或602 如何使用if-image-width370或602和if语句检查它 如果图像宽度是正确的,那么它是好的,否则我删除文件 下面的代码总是失败,即使维度不匹配。我如何使下面的内容允许任何一个维度 using System; public class Program { public static void Main() { Console.WriteLine("Hello World");
370或602
和if语句检查它
如果图像宽度是正确的,那么它是好的,否则我删除文件
下面的代码总是失败,即使维度不匹配。我如何使下面的内容允许任何一个维度
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
int imgW = 370; // assuming image width is 370
if (imgW != 370 || imgW != 602)
{
Console.WriteLine("One");
}
else
{
Console.WriteLine("Two");
}
}
}
根据你提到的: 如何使用if-image-width 370或602 with-if语句检查它 您需要的是
==
操作符来允许任意一个维度:
if (imgW == 370 || imgW == 602)
您可以创建允许宽度的集合,以使代码更具可读性和可扩展性:
var allowedWidths = new[]{ 370, 602 };
if(allowedWidths.Contains(imgW))
{
// upload
}
else
{
// delete
}
使用单值比较:
if(imgW == 370 || imgW == 602)
{
// upload
}
else
{
// delete
}
if(imgW==370 | | imgW==602)
?还是我误解了?改变或(| |)到和(&&&)。因此,如果图像的宽度不是370,也不是602,您将得到。代码应该是这样的:if(imgW!=370&&imgW!=602)
。这与if(!(imgW==370 | | imgW==602))
或if(imgW==370 | | imgW==602)
相同,但对于切换体,第二个选项很好,因为宽度只能是两个不同的维度