2016-06-23 49 views
1

所以我製作了一個小型控制檯計算器來計算圓的周長。但我想用方法重寫它。但我不知道從哪裏開始。誰能幫我使用方法來製作圓周計算器

using System; 

class Circle 
{ 
     static void Main() 
    // Circumference of a circle: C=2πr 
    { 
     Console.WriteLine("What is the radius of your circle: "); 
     double radius = double.Parse(Console.ReadLine()); 
     double pi = 3.1452; 
     double area = 2 * pi * radius; 
     Console.WriteLine("The Circumference of your circle is: "+ area); 
     Console.ReadKey(); 
    } 
} 
+0

你必須更清楚。你想用方法重寫什麼?你將如何分組功能?這裏提取的東西很少。 –

+0

使用我想分組數學部分(radius,pi,area)的方法並將Console.WriteLine部分分組。 –

回答

0
using System; 

class Circle 
{ 
    public static double GetCircumference(double radius) 
    { 
     return 2 * Math.PI * radius; 
    } 

    static void Main() 
    // Circumference of a circle: C=2πr 
    { 
     Console.WriteLine("What is the radius of your circle: "); 
     Console.WriteLine("The Circumference of your circle is: " + GetCircumference(Double.Parse(Console.ReadLine())).ToString()); 
     Console.ReadKey(); 
    } 
} 
+0

參數1:無法將字符串轉換爲雙行15 –

+0

@GeorgeTraynor我已經解決了該問題 –

+0

感謝您的幫助 –

0

如果你只是想打出來的計算成一個方法,你只需要創建下面的方法下方的主要方法:

private static double CalculateCircumference(double radius) 
    { 
     return 2 * radius * Math.PI; 
    } 

然後從調用它你的主要方法中,像這樣:

double circumference = CalculateCircumference(radius); 

總而言之

static void Main() 
    { 
     Print("What is the radius of your circle: "); 
     double radius = ParseInputNumber(); 
     double circumference = CalculateCircumference(radius); 
     Print("The Circumference of your circle is: " + circumference); 
     WaitForKeystroke(); 
    } 

    private static void Print(string message) 
    { 
     Console.WriteLine(message); 
    } 

    private static double ParseInputNumber() 
    { 
     return double.Parse(Console.ReadLine()); 
    } 
    private static void WaitForKeystroke() 
    { 
     Console.ReadKey(); 
    } 

    private static double CalculateCircumference(double radius) 
    { 
     return 2 * radius * Math.PI; 
    }