首页 > 解决方案 > 瓶装啤酒程序在插值后中断

问题描述

我只是试图进行插值以清理我的代码,它破坏了我的程序。我需要帮助理解它为什么会中断。更改后,递减停止。原始代码只是在每个 if() 上输入。更改前的原始代码在底部。带有运行的顶部代码,不再从 99 递减。

        int bottlesOfBeer = 99;
        string verse1 = $"{bottlesOfBeer} bottles of beer on the wall, {bottlesOfBeer} bottles of beer.";
        string verse2 = $"Take one down, pass it around {bottlesOfBeer} bottles of beer on the wall";
        string singleVerse1 = $"{bottlesOfBeer} bottle of beer on the wall, {bottlesOfBeer} bottle of beer.";
        string singleVerse2 = $"Take one down, pass it around {bottlesOfBeer} bottle of beer on the wall";
        while (bottlesOfBeer > 0)
        {
            if (bottlesOfBeer >= 2)
            {
                Console.WriteLine(verse1);
                bottlesOfBeer--;
                if (bottlesOfBeer == 1)
                {
                    Console.WriteLine(singleVerse2);
                }
                else
                {
                    Console.WriteLine(verse2);
                }
            }          
            else if (bottlesOfBeer == 1)
            {
                Console.WriteLine(singleVerse1);
                bottlesOfBeer--;
                if (bottlesOfBeer == 0)
                {
                    Console.WriteLine(verse1);
                    Console.WriteLine($"Check the back cause we need more");
                }
                else
                {
                    Console.WriteLine(singleVerse2);
                }**

     int bottlesOfBeer = 99;

        string verse1 = $"{bottlesOfBeer} bottles of beer on the wall, {bottlesOfBeer} bottles of beer.";
        string verse2 = $"Take one down, pass it around {bottlesOfBeer} bottles of beer on the wall";
        string singleVerse1 = $"Take one down, pass it around {bottlesOfBeer} bottle of beer on the wall";
        string singleVerse2 = $"Take one down, pass it around {bottlesOfBeer} bottle of beer on the wall";


        while (bottlesOfBeer > 0)
        {
            if (bottlesOfBeer >= 2)
            {
                Console.WriteLine($"{bottlesOfBeer} bottles of beer on the wall, {bottlesOfBeer} bottles of beer.");
                bottlesOfBeer--;
                if (bottlesOfBeer == 1)
                {
                    Console.WriteLine($"Take one down, pass it around {bottlesOfBeer} bottle of beer on the wall");
                }
                else
                {
                    Console.WriteLine($"Take one down, pass it around {bottlesOfBeer} bottles of beer on the wall");
                }
            }          
            else if (bottlesOfBeer == 1)
            {
                Console.WriteLine($"{bottlesOfBeer} bottle of beer on the wall, {bottlesOfBeer} bottle of beer.");
                bottlesOfBeer--;
                if (bottlesOfBeer == 0)
                {
                    Console.WriteLine($"{bottlesOfBeer} bottles of beer on the wall, {bottlesOfBeer} bottles of beer.");
                    Console.WriteLine($"Check the back cause we need more");
                }
                else
                {
                    Console.WriteLine($"Take one down, pass it around {bottlesOfBeer} bottle of beer on the wall");
                }

标签: c#while-loopinterpolation

解决方案


推荐阅读