2016-09-17 6 views
0

私はプログラミングが本当に新しいです。 C#は私が取った最初のクラスで、私はこのプロジェクトに固執しています。ワークショップラジオボタンと場所ラジオボタンを選択した後、ワークショップの費用を計算するプログラムを作成しなければなりませんでした。私はそれが1つの事を除いて仮定されているようにすべての作業をしています。ラジオボタンが選択されていない場合、プログラムの計算を停止するにはどうすればよいですか?

ワークショップを選択したが、場所を選択していないとします。私はMessageBoxが「場所を選択する」と表示される場所に持っていますが、このような場合にプログラムが計算を止めるにはどうしたらいいですか?今のところ、それはちょうど計算し、位置量0を与えるでしょう。私は計算しないためにそれが必要です。

public partial class frmWorkshopSelector : Form 
{ 

    public frmWorkshopSelector() 
    { 
     InitializeComponent(); 
     } 

    private void btnExit_Click(object sender, EventArgs e) 
    { 
     this.Close();  //When clicking the exit button, the program will close 
    } 

    private void btncalc_Click(object sender, EventArgs e) 
    { 
     int wsregistration = 0; 
     int lcost = 0; 
     const decimal DAYS = 3; 


     //For the following if statements, depending on what workshop and location is selected, 
     //their correstponding registration and lodging fees will be displayed 

     { 
      if (rbtHandlingStress.Checked == true) 
      { 
       wsregistration = 1000; 
      } 
      else if (rbtSupervisionSkills.Checked == true) 
      { 
       wsregistration = 1500; 
      } 
      else if (rbtTimeManagement.Checked == true) 
      { 
       wsregistration = 800; 
      } 

      else 
      MessageBox.Show("Please Select a Workshop"); 
      lblTotalCost.Text = ""; 
      lblLodgingCost.Text = ""; 
      lblRegistrationCost.Text = ""; 
     } 

     { 
      if (rbtAustin.Checked == true) 
      { 
       lcost = 150; 
      } 
      else if (rbtChicago.Checked == true) 
      { 
       lcost = 225; 
      } 
      else if (rbtDallas.Checked == true) 
      { 
       lcost = 175; 
      } 
      else 
      { 
       MessageBox.Show("Please Select a Location"); 
       lblRegistrationCost.Text = " "; 
       lblTotalCost.Text = " "; 
       lblLodgingCost.Text = " "; 
      } 
     } 

     lblRegistrationCost.Text = wsregistration.ToString("C"); 
     lblLodgingCost.Text = lcost.ToString("C"); 
     lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C"); 

    } 

    private void btnReset_Click(object sender, EventArgs e) 
    { 
     //unchecks all radio buttons as well as clears out the previous calculations 
     lblRegistrationCost.Text = ""; 
     lblLodgingCost.Text = ""; 
     lblTotalCost.Text = ""; 
     rbtHandlingStress.Checked = false; 
     rbtSupervisionSkills.Checked = false; 
     rbtTimeManagement.Checked = false; 
     rbtAustin.Checked = false; 
     rbtChicago.Checked = false; 
     rbtDallas.Checked = false; 
    } 
} 

答えて

1

このメソッドから終了する必要があります。 elseブロックにreturn文を追加しました。どこでも関数内で「戻る」の書き込み

private void btncalc_Click(object sender, EventArgs e) 
{ 
    int wsregistration = 0; 
    int lcost = 0; 
    const decimal DAYS = 3; 


    //For the following if statements, depending on what workshop and location is selected, 
    //their correstponding registration and lodging fees will be displayed 
    if (rbtHandlingStress.Checked == true) 
    { 
     wsregistration = 1000; 
    } 
    else if (rbtSupervisionSkills.Checked == true) 
    { 
     wsregistration = 1500; 
    } 
    else if (rbtTimeManagement.Checked == true) 
    { 
     wsregistration = 800; 
    } 

    else 
    {  
     lblTotalCost.Text = ""; 
     lblLodgingCost.Text = ""; 
     lblRegistrationCost.Text = ""; 
     MessageBox.Show("Please Select a Workshop"); 
     return; 
    } 


    if (rbtAustin.Checked == true) 
    { 
     lcost = 150; 
    } 
    else if (rbtChicago.Checked == true) 
    { 
     lcost = 225; 
    } 
    else if (rbtDallas.Checked == true) 
    { 
     lcost = 175; 
    } 
    else 
    {  
     lblRegistrationCost.Text = " "; 
     lblTotalCost.Text = " "; 
     lblLodgingCost.Text = " "; 
     MessageBox.Show("Please Select a Location"); 
     return; 
    } 

    lblRegistrationCost.Text = wsregistration.ToString("C"); 
    lblLodgingCost.Text = lcost.ToString("C"); 
    lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C"); 
} 
0

は、あなたが場所を入力するメッセージボックスを表示し、多分後、その機能を終了し、あなたは

return; 

を入力し、これが仕事をする必要があります。

0

だけ

public partial class frmWorkshopSelector : Form 
{ 

    public frmWorkshopSelector() 
    { 
     InitializeComponent(); 
     } 

    private void btnExit_Click(object sender, EventArgs e) 
    { 
     this.Close();  //When clicking the exit button, the program will close 
    } 

    private void btncalc_Click(object sender, EventArgs e) 
    { 
     int wsregistration = 0; 
     int lcost = 0; 
     const decimal DAYS = 3; 


     //For the following if statements, depending on what workshop and location is selected, 
     //their correstponding registration and lodging fees will be displayed 

     { 
      if (rbtHandlingStress.Checked == true) 
      { 
       wsregistration = 1000; 
      } 
      else if (rbtSupervisionSkills.Checked == true) 
      { 
       wsregistration = 1500; 
      } 
      else if (rbtTimeManagement.Checked == true) 
      { 
       wsregistration = 800; 
      } 

      else 
      MessageBox.Show("Please Select a Workshop"); 
      lblTotalCost.Text = ""; 
      lblLodgingCost.Text = ""; 
      lblRegistrationCost.Text = ""; 
      return; 
     } 

     { 
      if (rbtAustin.Checked == true) 
      { 
       lcost = 150; 
      } 
      else if (rbtChicago.Checked == true) 
      { 
       lcost = 225; 
      } 
      else if (rbtDallas.Checked == true) 
      { 
       lcost = 175; 
      } 
      else 
      { 
       MessageBox.Show("Please Select a Location"); 
       lblRegistrationCost.Text = " "; 
       lblTotalCost.Text = " "; 
       lblLodgingCost.Text = " "; 
       return; 
      } 
     } 

     lblRegistrationCost.Text = wsregistration.ToString("C"); 
     lblLodgingCost.Text = lcost.ToString("C"); 
     lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C"); 

} 


    private void btnReset_Click(object sender, EventArgs e) 
    { //uncheks all radio buttons as well as clears out the previous calculations 
     lblRegistrationCost.Text = ""; 
     lblLodgingCost.Text = ""; 
     lblTotalCost.Text = ""; 
     rbtHandlingStress.Checked = false; 
     rbtSupervisionSkills.Checked = false; 
     rbtTimeManagement.Checked = false; 
     rbtAustin.Checked = false; 
     rbtChicago.Checked = false; 
     rbtDallas.Checked = false; 
    } 
} 

}

以下のようなメッセージボックスを表示した後、あなたのコード内でreturnステートメントを追加します。
関連する問題