0

嗨,我是 mirosoft Visual Studio 的新手,我想创建一个基本圆圈,但出现以下错误:无法将类型“double”隐式转换为 int。存在显式转换。(你错过了演员吗?)

 using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {


                int radius = Convert.ToInt32(textBox1.Text);

                int circumference = 2 * Math.PI * radius;
                int area = Math.PI * Math.Pow(radius, 2);
                int volume = (4 * Math.PI / 3) * Math.Pow(radius, 3);

                Graphics paper;
                paper = pictureBox1.CreateGraphics();
                Pen pen = new Pen(Color.Red);

                paper.DrawEllipse(pen, 0, circumference, area, volume);
            }
        }
    }
4

2 回答 2

1
int straal = Convert.ToInt32(textBox1.Text);
double omtrek = 2 * Math.PI * straal;
        double oppervlakte = Math.PI * Math.Pow(straal, 2);
        double volume = (4 * Math.PI / 3) * Math.Pow(straal, 3);

        Graphics paper;
        paper = pictureBox1.CreateGraphics();
        Pen pen = new Pen(Color.Red);

        paper.DrawEllipse(pen, 0, Convert.ToInt32(omtrek), Convert.ToInt32(oppervlakte),   Convert.ToInt32(volume));
于 2012-10-01T16:08:01.017 回答
0

如果你想将一个 double 转换为一个 int,你可以使用这样的括号:

double myDouble = 3.211;
int myInt = (int)myDouble;

在您的上下文中:

            int circumference = (int)(2 * Math.PI * radius);
            int area = (int)(Math.PI * Math.Pow(radius, 2));
            int volume = (int)((4 * Math.PI / 3) * Math.Pow(radius, 3));
于 2012-10-01T16:00:05.737 回答