-4

这是我第一次使用这个论坛!我是一名大学二年级学生,刚刚开始用 C# 编写代码(就像我们去年做 java 一样)。

其中一个实验练习是编写一个小程序,它会弹出一个终端窗口,要求输入一个数字(十进制数),这意味着程序通过调用另一个类的方法来计算面积的半径!

我已经在 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 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 回答