2017-02-19 7 views
2

私はクラスのBMI計算機を作成する必要がありますが、いくつかの助けが必要です。私はその後、計算値2に入れて私のプログラムを実行すると、Cで小数点以下2桁だけを表示する方法#

  1. 、それが正しい答えが表示されますが、小数点以下8桁のような存在です。

  2. 数値以外の入力をするとプログラムがクラッシュしますが、これをどのように修正しますか?

は、ここに私のコードです:

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 WindowsFormsApplication3 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void weightTxt_TextChanged(object sender, EventArgs e) 
    { 


    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     double BMI = 0; 
     double weight = 0; 
     double height = 0; 
     height = Double.Parse(heightTxt.Text); 
     weight = Double.Parse(weightTxt.Text); 
     // declaring and assigning 
     if (weight > 300 || weight < 10) 
     { 
      MessageBox.Show("Not a valid input."); 

     } 
     if (height > 2.2 || height < 0.2) 
     { 
      MessageBox.Show("Not a valid input."); 
     } 

     // checking that values meet parameters 
     BMI = weight/(height * height); 
     string result = Convert.ToString(BMI); 
     resultLbl.Text = "Your BMI is : " + result; 
+1

'文字列結果は= BMI.ToString( "###。");' – shash678

答えて

1

非数値の入力時にクラッシュする問題に対処するために、あなたの代わりにDouble.ParseのDouble.TryParseを使用することができます。

if (!Double.TryParse(heightTxt.Text, out height)) { 
    MessageBox.Show("Not a valid input."); 
    return; 
} 

小数点以下2桁まで表示する問題に対処するには、BMI.ToString( "#。##")を使用してください。他の人がコメントしたように

+0

これは働いていたが、なぜ文書にLOLおかげで、病気の表情私はわからないんだけど – Sal

0
 double weight = 0; 
     double height = 0; 

     if (Double.TryParse(weightTxt.Text, out weight) && Double.TryParse(heightTxt.Text, out height)) { 
      if (weight > 300 || weight < 10 || height > 2.2 || height < 0.2) 
      { 
       MessageBox.Show("Not a valid input."); 
      } 

      double BMI = weight/(height * height); 

      /*Two ways for converting to two didges:*/ 

      // 1. 

      // Round to two didgets 
      double result = Math.Round(BMI, 2); 
      // convert result to string 
      string resultString = Convert.ToString(result); 

      // 2. 
      string resultString = BMI.ToString("#.##")    
     }  
0

少年は、stackoverflowで宿題ごとにニッケルがある場合。

  double BMI = 0; 
      double weight = 0; 
      double height = 0; 

      //use try parse to test if the value is convertable 
      if (!Decimal.TryParse(heightTxt.Text,out height)) 
      { 
       MessageBox.Show("Not a valid input."); 
       return; 
      } 

      if (!Decimal.TryParse(weightTxt.Text, out weight)) 
      { 
       MessageBox.Show("Not a valid input."); 
       return; 
      } 

      // declaring and assigning 
      if (weight > 300 || weight < 10) 
      { 
       MessageBox.Show("Not a valid input."); 
       //return so the method dosnt try to do the rest of the code 
       return; 

      } 
      if (height > 2.2 || height < 0.2) 
      { 
       MessageBox.Show("Not a valid input."); 
       return; 
      } 

      BMI = weight/(height * height); 
      //this is how you format the nuumber to two decimal places 
      string result = BMI.ToString("#.##"); 
      resultLbl.Text = "Your BMI is : " + result; 
関連する問題