4

MicroSoft Visual Studio 2010 のコンソール プログラムに C# を使用して、皆さんの助けを借りてこれにいくつかの変更を加えましたが、このコンソール プログラムは正しく実行されています。ただし、main メソッドの出力セクションに「To Obey the Girl Scout Law」というモットーを表示する定数静的フィールドを実装する必要があります。それは簡単なものでなければならないことはわかっていますので、ご容赦ください。public static const string Motto = "To Obey the Girl Scout Law" をベース クラスに含めると、エラー メッセージが表示されます - The constant 'DemoScouts.GirlScout.Motto' can't be static. 以下は、このプロジェクトの完全なコードです。

パブリック クラス ガールスカウト {

    public static const string Motto = "To Obey the Girl Scout Law";  

    public static string scoutName;
    public static string enterName()
    {
        return scoutName;
    }

    public static int duesOwed;
    public static int enterAmount()
    {
        return duesOwed;
    }

    public static int troopNumber;
    public static int enterNumber()
    {
        return troopNumber;
    }
}

class MainClass : GirlScout
{
    static void Main()
    {
        Console.WriteLine();
        Console.Write("Enter the Girl Scout's name: ");
        GirlScout.scoutName = Console.ReadLine();
        Console.WriteLine();

        Console.Write("Enter their Troop Number: ");
        string n = Console.ReadLine();
        GirlScout.troopNumber = Int32.Parse(n);
        GirlScout.enterNumber();
        Console.WriteLine();

        Console.Write("Enter the amount they Owe in Dues: $");
        string d = Console.ReadLine();
        GirlScout.duesOwed = Int32.Parse(d);
        GirlScout.enterAmount();
        Console.WriteLine();

        // Seperate the input from the output:
        Console.WriteLine();
        Console.WriteLine(GirlScout.Motto);
        Console.WriteLine("-----------------------------------------------");
        Console.WriteLine();

        // Display the new information:
        Console.WriteLine("The name of the Girl Scout is: {0}", GirlScout.scoutName);
        Console.WriteLine("The troop Number of the Girl Scout is:   {0}", GirlScout.troopNumber);
        Console.WriteLine("The amount of Dues Owed by this Girl Scout is: {0}", GirlScout.duesOwed);

        // Keep the console window open in debug mode.
        Console.ReadKey();
    }
}

}

あらゆるアドバイスをいただければ幸いです。

4

4 に答える 4

6

ユーザーが入力した名前で何もしていません:

Console.Write("Enter the Girl Scout's name: ");
Console.ReadLine();

これは次のようになります。

Console.Write("Enter the Girl Scout's name: ");
GirlScout.scoutName = Console.ReadLine();

scoutNameのタイプをstringではなくに変更する必要もありintます。


また、クラスを再設計する必要があります。静的フィールドではなくインスタンス プロパティを使用します。

public class GirlScout
{
    public string Motto { get; set; }
    public string ScoutName { get; set; }
    public int DuesOwed { get; set; }
    public int TroopNumber { get; set; }
}
于 2012-07-01T18:42:45.770 に答える
4

他のデータと同様に、ユーザーが入力した名前を保存していません。

于 2012-07-01T18:42:31.403 に答える
4

への割り当てが表示されませんGirlScout.scoutName...

補足: 静的プロパティを使用しないことを検討してください (割り当ての目的でない限り)。通常のプロパティでオブジェクトを作成するか、それらをまったく使用しないでください...

于 2012-07-01T18:43:45.083 に答える
1

scoutName の値を初期化していないので、正確には何を出力すると予想されますか?

次のようなコード行が欠落していると思います。

GirlScout.scoutName = Console.ReadLine();

ただし、あなたのクラスは非常に貧弱に設計されていると言わざるを得ません。パブリック データ メンバーがあり、メソッドには目的や意味がないようです。カプセル化をブラッシュアップし、変数をプライベートにする必要があります。メソッドを使用して値を変更/取得します。

ヘルプが必要な場合は、コメントまたは別の質問で質問してください。

于 2012-07-01T18:45:13.333 に答える