【问题标题】:C# no implicit fall through in switch statementC# 在 switch 语句中没有隐式失败
【发布时间】:2015-02-21 13:13:02
【问题描述】:

我来自 C++ 背景。我最近编写了一个程序,它提供了有关特定卢比的音符数量的信息。当它要求特定数量时需要。 以下是我的代码:

#include <iostream>
using std::cout;
using std::cin;
int main()
{
    int amount,notes,choice;
    cout<<"Enter the amount: ";
    cin>>amount;
    cout<<"Enter the value of note from which you want to start: ";
    cin>>choice;
    switch(choice)
    {
        case 100:
            notes=amount/100;
            cout<<"no of 100 rupees notes = "<<notes<<'\n';
            amount=amount%100;
        case 50:
            notes=amount/50;
            cout<<"no of 50 rupees notes = "<<notes<<'\n';
            amount=amount%50;
        case 20:
            notes=amount/20;
            cout<<"no of 20 rupees notes = "<<notes<<'\n';
            amount=amount%20;
        case 10:
            notes=amount/10;
            cout<<"no of 10 rupees notes = "<<notes<<'\n';
            amount=amount%10;
        case 5:
            notes=amount/5;
            cout<<"no of 5 rupees notes = "<<notes<<'\n';   
            amount=amount%5;
        case 2:
            notes=amount/2;
            cout<<"no of 2 rupees notes = "<<notes<<'\n';
            amount=amount%2;
        case 1:
            notes=amount/1;
            cout<<"no of 1 rupees notes = "<<notes<<'\n';
            break;
        default:
            cout<<"Enter only valid values";
    }
    return 0;
}

现在我的问题是如何用 C# 编写这个程序?在 C# 中没有隐式的失败案例,但在这个程序中,需要隐式的失败案例。那么我可以通过哪些方式在 C# 中执行此程序? 请帮帮我。

【问题讨论】:

  • 您是否受限于在解决方案中使用switch 语句?
  • 失败是我在 30 多年的 IT 行业中见过的最愚蠢的事情之一,也是糟糕的编程风格的明确标志,imnsho

标签: c# c++ switch-statement


【解决方案1】:

Dennis_E 为您提供了最简单的更改,但您可以通过使用循环结构来避免 goto 和大量重复 :)

例如:

using System;

namespace CurrencyNotes
{
    class Program
    {
        static void Main(string[] args)
        {
            int amount;
            int choice;

            Console.Write("Enter the amount: ");
            amount = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the value of note from which you want to start: ");
            choice = Convert.ToInt32(Console.ReadLine());

            CountNotes(amount, choice);
        }

        static void CountNotes(int amount, int choice)
        {
            int notes = 0;
            int[] choices = { 100, 50, 20, 10, 5, 2, 1 };

            // Find starting choice
            int i = 0;
            while (choice < choices[i])
                ++i;

            // Output number of notes for each suitable choice
            while (amount > 0)
            {
                notes = amount / choices[i];
                if (notes > 0)
                    Console.WriteLine("no. of {0} rupees notes = {1}", choices[i], notes);
                amount %= choices[i];
                ++i;
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    原因是隐式掉线是导致细微错误的原因,当你忘记添加break;时,如果需要掉线,可以使用goto:

    case 100:
        notes=amount/100;
        cout<<"no of 100 rupees notes = "<<notes<<'\n';
        amount=amount%100;
        goto case 50;
    

    【讨论】:

    • 任何其他不涉及使用 goto 的方式,因为使用 goto 会导致非结构化代码和难以理解的代码
    • 所有其他跳转语句也可以这样说。 goto 在某些情况下有其用途。在 C# 文档中,这是推荐的失败方式。
    • @meet 我不知道在这种情况下如何。您不愿使用goto 似乎是出于对它的仇恨,而不是经验。 goto 有时是一个完全有效的解决方案。
    【解决方案3】:

    就个人而言,我倾向于采用完全不同的方法。通过数组而不是开关。

    类似于:

    using System;
    
    namespace Test
    {
        class MainClass
        {
            public static void Main (string[] args)
            {
                int[] notes = new int[] { 100, 50, 20, 10, 5, 2, 1 };
                int amount = 0;
    
                amount = Convert.ToInt32 (Console.ReadLine ());
                foreach (int i in notes) {
                    Console.WriteLine (amount / i);
                    amount = amount % i;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多