简体   繁体   中英

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) ? 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 (monthentered == 1 || monthentered == 3 || monthentered == 5 /*|| ...*/)

You need to check value of variable every time so it will be while(bool || bool || bool) etc

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'

You can't use || on numbers - each part you are using || with should evaluate to a 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 || operator . monthentered == 01 is a bool (true/false) and the rest are ints .

So change the while to:

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.
  • your while loops on months will never terminate, as you never change the value of the month. Use an if statement instead.

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. This will also accommodate leap years.

It's tedious syntax but you would need to write the while statement as:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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