将ReadLine输入用于其他方法

时间:2016-09-23 10:46:29

标签: c#

我试图编写一个程序,根据我选择的变量,在屏幕上描绘一个笑脸。

我已经写了以下内容。

现在我的程序会问三个问题,我将用0到11之间的数字回答。我想在方法' tekenscherm'中使用这些答案。如何在该方法中调用这些变量?

class HalloForm : Form
{
    public string a,b,c = ""; //Declare them here.
    public HalloForm()
    {
        this.Text = "Hallo";
        this.BackColor = Color.Yellow;
        this.Size = new Size(800, 600);
        this.Paint += this.tekenScherm;
    }

    public static void Main()
    {
        System.Console.WriteLine("Smiley size, on a scale of 1 tot 10?");
        string a = System.Console.ReadLine();
        Int32.Parse(a);
        System.Console.WriteLine("X coordinate of the smiley, on a scale of 1 to 10");
        string b = System.Console.ReadLine();
        Int32.Parse(b);
        System.Console.WriteLine("Y coordinate of the smiley, on a scale of 1 to 10");
        string c = System.Console.ReadLine();
        Int32.Parse(c);
        HalloForm scherm;
        scherm = new HalloForm();

        Application.Run(scherm);
    }
    void tekenScherm(object obj, PaintEventArgs pea)
    {

        SolidBrush blueBrush = new SolidBrush(Color.Blue);
        Pen blackBrush = new Pen(Color.Black, 5);
        int x = 360;
        int y = x + 75;
        pea.Graphics.FillEllipse(blueBrush, 300, 200, 200, 200);
        pea.Graphics.DrawEllipse(blackBrush, 300, 200, 200, 200);
        pea.Graphics.DrawArc(blackBrush, 350, 250, 100, 100, 45, 90);
        pea.Graphics.DrawEllipse(blackBrush, a, 250, 5, 5); //I've used it here
        pea.Graphics.DrawEllipse(blackBrush, y, 250, 5, 5);
    }
}

1 个答案:

答案 0 :(得分:0)

将方法范围之外的变量声明为整数。

Int32.Parse返回一个整数值,你没有将这些值赋给这些变量,所以它基本上没有按照你使用它的方式做任何事情。

以下是工作代码:

class HalloForm : Form
{
    public int a, b, c = 0; //Declare them here.
    public HalloForm()
    {
        this.Text = "Hallo";
        this.BackColor = Color.Yellow;
        this.Size = new Size(800, 600);
        this.Paint += this.tekenScherm;
    }

    public static void Main()
    {
        System.Console.WriteLine("Smiley size, on a scale of 1 tot 10?");
        a = Int32.Parse(System.Console.ReadLine());
        System.Console.WriteLine("X coordinate of the smiley, on a scale of 1 to 10");
        b = Int32.Parse(System.Console.ReadLine());
        System.Console.WriteLine("Y coordinate of the smiley, on a scale of 1 to 10");
        c = Int32.Parse(System.Console.ReadLine());
        HalloForm scherm;
        scherm = new HalloForm();

        Application.Run(scherm);
    }
    void tekenScherm(object obj, PaintEventArgs pea)
    {

        SolidBrush blueBrush = new SolidBrush(Color.Blue);
        Pen blackBrush = new Pen(Color.Black, 5);
        int x = 360;
        int y = x + 75;
        pea.Graphics.FillEllipse(blueBrush, 300, 200, 200, 200);
        pea.Graphics.DrawEllipse(blackBrush, 300, 200, 200, 200);
        pea.Graphics.DrawArc(blackBrush, 350, 250, 100, 100, 45, 90);
        pea.Graphics.DrawEllipse(blackBrush, x, 250, 5, 5);
        pea.Graphics.DrawEllipse(blackBrush, y, 250, 5, 5);
    }
}