2012-10-01 34 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));