-4

初めてこのフォーラムを利用しました!私は大学 2 年生で、C# でコードを書き始めたばかりです (昨年は Java をやったように)。

ラボ演習の 1 つは、端末ウィンドウをポップアップ表示して数値 (10 進数) を要求する小さなプログラムを作成することです。これは、プログラムが別のクラスからメソッドを呼び出して面積を計算する半径を意味します。

同じ名前空間を使用して Visual Studio 2008 でコードを作成しましたが、ビルドして実行できますが、動作しませんか? これがさまざまなクラスのコードです。ヘルプ/アドバイスをいただければ幸いです。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter The Radius:");//Text to be displayed in the console
            Console.ReadLine();//Read the line and store information in temp directory
            Pie one = new Pie();//Calls the method from the class in the same namespace
            Console.ReadKey();//Reads and displays the next key pressed in the console   
            Environment.Exit(0);//Exit the Enviromet        
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program4
{
    class Pie
    {    
        public void Pin ()
        {
            int r;//defining the value that is going to be entered as an integer 
            double result;//declaring the result string as a double
            r = (int)Convert.ToDouble(Console.ReadLine());
            result=(3.14*r*r);//the calculation to work out pie
            Console.WriteLine("The Radius of the circle is " + (result));//the writeline statement    
         }
    }
}
4

2 に答える 2

3

コードを実行してみてください:

Pie one = new Pie();
one.Pin();

また:
この行:

Console.ReadLine();//Read the line and store information in temp directory

そのコメントは非常に間違っています。そのはず//Read the line and throws the result away

そしてこれ:これ(int)Convert.ToDouble(Console.ReadLine());
に置き換えることができます:int.Parse(Console.ReadLine())

于 2013-10-10T09:24:03.253 に答える
-4

クラス Pie と public void Pin() に static を追加します。それが動作します

    static class Pie
    {

          public static void Pin ()
          {
            int r;//defining the value that is going to be entered as an integer 
            double result;//declaring the result string as a double
            r = (int)Convert.ToDouble(Console.ReadLine());
            result=(3.14*r*r);//the calculation to work out pie
            Console.WriteLine("The Radius of the circle is " + (result));//the writeline statement    
         }
    }

または、必要に応じて、クラスをインスタンス化してから、そのようなメソッドを呼び出すことができます

Pie pie=new Pie();
pie.Pin();
于 2013-10-10T09:21:36.907 に答える