这是我第一次使用这个论坛!我是大学二年级学生,刚刚开始用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
}
}
}
答案 0 :(得分: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())
答案 1 :(得分:-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();