2012-10-01 36 views
0

嗨,我是全新的mirosoft视觉工作室,我想创建一个基本的圆形,但我有以下错误:不能将类型'双'转换为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); 
      } 
     } 
    } 

回答

0

如果你想投双为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)); 
+0

是否有可能只做双打?我尝试了这种方式,但然后我在我的最后一行“paper.DrawEllipse(笔,0,周长,面积,体积)有一个错误;”说它必须是诠释。 – mrName

+0

你也必须在其他部分也加上括号 – MikeB

+0

谢谢大家回答我的问题,你们都帮助我解决了我的问题,我真的赞赏你在解决我的问题时付出的努力和时间;) – mrName

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