Home > OS >  C# while loop it repeats 1bottles and 1 bottle I would like to know how to remove the 1bottles line
C# while loop it repeats 1bottles and 1 bottle I would like to know how to remove the 1bottles line

Time:01-11

Hi guys I need help solving a c# problem. This is the code being run as you can see it repeats 1bottles and 1 bottle on the picture I would like to know how to remove the "1 bottles on the wall take one down" and keep "1 bottle on the wall" please help

class Program
{
    static void Main(string[] args)
    {
        string strBottles = "bottles";
        int bottles = 100;           

        while (bottles <= 100)
        {
            Console.WriteLine(bottles   strBottles   " on the wall"   " take one down");              
                           
            if(bottles == 1)
            {
                strBottles = "bottle";
                Console.WriteLine(bottles   strBottles   " on the wall");
                break;
            }

            bottles--;
        }
    }    
}

and this image is the code being run

CodePudding user response:

You need to check the value before the first message printing, add an if check like so,

if(bottles>1)
  Console.WriteLine(bottles   strBottles   " on the wall"   " take one down");

CodePudding user response:

Choosing a possible infinite do/while with a break condition that can or cannot be fullfilled is a bad idea I will try this aproach:

        string strBottle = "bottle";
        int iBottles = 100;
        for (int bottles = iBottles; bottles >=1; bottles--)
        {
            Console.WriteLine(bottles   strBottle   (bottles>1?"s":"")  " on the wall"   (bottles > 1 ? " take one down":""));
        }

CodePudding user response:

Think about what the code is doing when bottles == 1.

First it hits this line

Console.WriteLine(bottles   strBottles   " on the wall"   " take one down");

then it enters the if statement:

if(bottles == 1)
{
    strBottles = "bottle";
    Console.WriteLine(bottles   strBottles   " on the wall");
    break;
}

If you want only the "1bottle on the wall", then the if statement needs to come first:

while (bottles > 0)
{
    if(bottles == 1)
    {
        Console.WriteLine($"{bottles} bottle on the wall");
        break;
    }
    Console.WriteLine($"{bottles} bottles on the wall take one down");                             
    
    bottles--;
}

Other improvements:

  • while (bottles <= 100) - bottles starts at 100 and decreases so this statement will always be true. If you changed the initial number of bottles to int bottles = 101 the code wouldn't execute the while loop in your version. You never want it to go below 1 so the condition should be while (bottles > 0)
  • You don't really need the string strBottles, it's redundant and can be removed
  • If you're using C# 6.0 or above take a look at string interpolation. It provides a more readable way to format strings with expressions: $"{bottles} bottles on the wall"
  •  Tags:  
  • Related