0
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'"

请帮忙。

4

9 回答 9

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

|| 之间的每一个操作 并且 && 必须呈现为布尔值(即完整的测试),因为每个操作都是独立计算的

于 2012-11-22T16:20:57.347 回答
2

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

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

你需要每次检查变量的值,所以它会是while(bool || bool || bool)

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

于 2012-11-22T16:21:29.443 回答
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))
于 2012-11-22T16:21:46.067 回答
1

您不能将 anintboolwith||运算符进行比较。monthentered == 01bool(真/假),其余的是ints

因此,将 while 更改为:

int[] allAllowedMonths = new[]{ 1, 3, 5, 7, 8, 10, 12 };
while (allAllowedMonths.Contains( monthentered ))
{
    // ...
}
于 2012-11-22T16:22:39.847 回答
1

它应该是

while (monthentered  == 01 ||monthentered  == 03 || monthentered  ==05 || monthentered  ==07 || monthentered  ==08 || monthentered  ==10 || monthentered  ==12)
于 2012-11-22T16:20:49.800 回答
0

它应该是

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

 {  
   //code here
 }
于 2012-11-22T16:22:36.990 回答
0

导致代码编译不起作用的直接答案:您需要使用

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

进一步的问题:

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

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

于 2012-11-22T16:22:39.877 回答
0

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

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

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

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

于 2012-11-22T16:25:11.267 回答
0

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

while (monthentered == 01 || monthentered ==03 || monthentered ==05 || monthentered ==07 || monthentered ==08 || monthentered ==10 || monthentered ==12) 
于 2012-11-22T16:22:13.090 回答