简体   繁体   English

我的“ while”陈述出了什么问题

[英]Whats wrong with my “while” statement

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();

Is there anything wrong with line while (monthentered == 01 || 03 || 05 || 07 || 08 || 10 || 12) ? while (monthentered == 01 || 03 || 05 || 07 || 08 || 10 || 12)时行有什么问题吗? I getting an error that I don't understand. 我收到一个我不明白的错误。 "Operator '||' cannot be applied to operands of type 'bool' and 'int'"

Please help. 请帮忙。

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

Every operation between || ||之间的每个操作 and && have to render to a boolean value (ie a complete test) since every operation is independently calculated 和&&必须渲染为布尔值(即完整测试),因为每个操作都是独立计算的

Now it is something like while(bool || int || int) , I guess that you want something like this: 现在就像while(bool || int || int) ,我想您想要这样的东西:

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

You need to check value of variable every time so it will be while(bool || bool || bool) etc 您需要每次检查变量的值,因此它会是while(bool || bool || bool)

You can also create collection of valid months and check wheter entered number is in it. 您还可以创建有效月份的集合,并检查其中输入的数字。

Operator '||' 运算符'||' cannot be applied to operands of type 'bool' and 'int' 不能应用于'bool'和'int'类型的操作数

You can't use || 您不能使用|| on numbers - each part you are using || 在数字上-您正在使用的每个部分|| with should evaluate to a bool . 与应该评估为bool

The conditional should look like: 条件应如下所示:

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

Though a more readable option would be: 尽管更易读的选项是:

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

while(validMonths.Contains(monthentered))

它应该是

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

You cannot compare an int with a bool with || 您不能将||boolint进行比较。 operator . 操作员 monthentered == 01 is a bool (true/false) and the rest are ints . monthentered == 01bool (true / false),其余均为ints

So change the while to: 因此,将while更改为:

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

C#同时仅接受true / false表达式!

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

it should be 它应该是

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

 {  
   //code here
 }

Direct answer that leads to code that compiles but won't work : you need to use 直接答案会导致代码可编译不起作用 :您需要使用

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

Further issues: 进一步的问题:

  • your code allows 32+ days in most months. 您的代码在大多数月份中需要32天以上的时间。
  • your while loops on months will never terminate, as you never change the value of the month. 您月份的while循环永远不会终止,因为您永远不会更改月份的值。 Use an if statement instead. 改用if语句。

Extra hint: you can use DateTime.DaysInMonth to get the maximum number to allow for each month and year, rather than manually checking the month number. 额外提示:您可以使用DateTime.DaysInMonth来获取每个月和每年允许的最大数字,而不是手动检查月份数。 This will also accommodate leap years. 这也将适应leap年。

It's tedious syntax but you would need to write the while statement as: 这是乏味的语法,但您需要将while语句编写为:

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

Your statement will first evaluate the boolean condition monthentered == 01 and then try to logically or the result with the other numbers which are ints hence the error. 您的语句将首先对布尔条件monthentered == 01进行求值,然后尝试对其他整数进行逻辑运算或得出结果,从而得出错误。

However I'd take a look at what you're trying to do and see if a while loop is really what you want here. 但是,我将看一下您要尝试执行的操作,看看在这里是否真的需要while循环。

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

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