c# - 瓶装啤酒程序在插值后中断
问题描述
我只是试图进行插值以清理我的代码,它破坏了我的程序。我需要帮助理解它为什么会中断。更改后,递减停止。原始代码只是在每个 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");
}
解决方案
推荐阅读
- ios - 提高 Swift 循环性能
- rust - WSL Ubuntu Cargo Rust openssl 环境变量未设置
- python - 按输出值对 seaborn 条形图进行排序
- android - 如何用frida拦截Android应用Intent?
- android - kotlin 范围功能“应用”如何在幕后工作?
- python - 显示输入数据集中的特定列
- javascript - 检查选择了哪个单选选项并根据该选项显示一个按钮
- reactjs - 以最短时间显示加载微调器
- typescript - React Native 中的参数装饰器
- android - Android Studio 在运行时给我一个“错误膨胀类布局”