我来自C++背景。我最近写了一个程序,该程序提供有关特定Rs多少音符的信息。要求特定金额时需要。
我的代码没有进一步说明以下内容:

#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#进行此程序的哪些方式呢?
请帮我。

最佳答案

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;
            }
        }
    }
}

09-27 08:13