我的“while”声明出了什么问题

时间:2012-11-22 16:19:18

标签: c# validation date

int monthentered = 0;
int dayentered = 0;
int year = 0000;
int [] month = new int [12];
int [] day = new int [31];
bool leap = false;

for (int x = 0; x <= 11; x++)
{
    month[x] = x+1;
}

for (int x = 0; x <= 30; x++)
{
    day[x] = x+1;
}
Console.WriteLine("Please enter a year...");
year = (Convert.ToInt16(Console.ReadLine()));
Console.WriteLine("{0}", year);

Console.WriteLine("Please enter a month...");
monthentered = (Convert.ToInt16(Console.ReadLine()));

Console.WriteLine("Please enter a day...");
dayentered = (Convert.ToInt16(Console.ReadLine()));

while (monthentered == 01 || 03 || 05 || 07 || 08 || 10 || 12)
{
    while (dayentered == 31)
    {
        Console.WriteLine("There are only 30 days in this month please re-enter your day...");
        dayentered = (Convert.ToInt16(Console.ReadLine()));
    }
}

while (monthentered == 02)
{
    while (dayentered > 28)
    {
        Console.WriteLine("There are only 28 days in this month please re-enter your day...");
        dayentered = (Convert.ToInt16(Console.ReadLine()));
    }
}

Console.WriteLine("{0}/{1}/{2}", dayentered, monthentered, year);
Console.ReadKey();

while (monthentered == 01 || 03 || 05 || 07 || 08 || 10 || 12)行有什么问题吗?我收到一个我不理解的错误。 "Operator '||' cannot be applied to operands of type 'bool' and 'int'"

请帮忙。

9 个答案:

答案 0 :(得分:3)

while(monthentered == 01 || monthentered == 03 || monthentered == 05 || monthentered == 07 || monthentered == 08 || monthentered == 10 || monthentered == 12)

||之间的每次操作和&amp;&amp;必须渲染到布尔值(即完整的测试),因为每个操作都是独立计算的

答案 1 :(得分:2)

现在它就像while(bool || int || int),我想你想要这样的东西:

while (monthentered == 1 || monthentered == 3 || monthentered == 5 /*|| ...*/)

您需要每次都检查变量的值,因此它将是while(bool || bool || bool)

您还可以创建有效月份的集合,并检查输入的数字是否在其中。

答案 2 :(得分:2)

  

运营商'||'不能应用于'bool'和'int'

类型的操作数

您无法在数字上使用|| - 您使用||的每个部分都应评估为bool

条件应该如下:

while (monthentered == 01 || 
       monthentered == 03 || 
       monthentered == 05 || 
       monthentered == 07 || 
       monthentered == 08 || 
       monthentered == 10 || 
       monthentered == 12)

虽然更可读的选项是:

var validMonths = new int[] { 1, 3, 5, 7, 8, 10, 12 };

while(validMonths.Contains(monthentered))

答案 3 :(得分:1)

应该是

while (monthentered  == 01 ||monthentered  == 03 || monthentered  ==05 || monthentered  ==07 || monthentered  ==08 || monthentered  ==10 || monthentered  ==12)

答案 4 :(得分:1)

您无法将intbool|| operator进行比较。 monthentered == 01bool(真/假),其余为ints

所以将while改为:

int[] allAllowedMonths = new[]{ 1, 3, 5, 7, 8, 10, 12 };
while (allAllowedMonths.Contains( monthentered ))
{
    // ...
}

答案 5 :(得分:0)

C#,只接受真/假表达式!

while (monthentered == 01 || monthentered ==03 || monthentered ==05 || monthentered ==07 || monthentered ==08 || monthentered ==10 || monthentered ==12) 

答案 6 :(得分:0)

应该是

while (monthentered == 01 || 
       monthentered == 03 || 
       monthentered == 05 || 
       monthentered == 07 || 
       monthentered == 08 || 
       monthentered == 10 || 
       monthentered == 12)

 {  
   //code here
 }

答案 7 :(得分:0)

直接回答导致代码编译但不会工作:您需要使用

(monthentered == 01 || monthentered == 03 || monthentered == 05 ||
 monthentered == 07 || monthentered == 08 || monthentered == 10 ||
 monthentered == 12)

其他问题:

  • 您的代码在大多数月份内允许超过32天。
  • 月份上的while循环永远不会终止,因为您永远不会更改月份的值。请改用if语句。

额外提示:您可以使用DateTime.DaysInMonth获取每月和每年允许的最大数量,而不是手动检查月份数。这也将适应闰年。

答案 8 :(得分:0)

这是繁琐的语法,但您需要将while语句编写为:

while (monthentered == 01 || monthentered == 02 || monthentered == 3 ...等

您的语句将首先评估布尔条件monthentered == 01,然后尝试逻辑或结果与其他数字一起生成因此错误。

但是,我会看看你想要做什么,看看while循环是否真的是你想要的。