使用不同方法的ReadLine输入

本文关键字:ReadLine 输入 方法 | 更新日期: 2023-09-27 18:08:23

我正在尝试编写一个程序,给定我选择的变量,在屏幕上描绘一个笑脸。

我写了以下内容。

现在我的程序问了三个问题,我将用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);
    }
}

使用不同方法的ReadLine输入

将方法作用域之外的变量声明为整数

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);
    }
}