【问题标题】:Returning Int from If statement从 If 语句返回 Int
【发布时间】:2017-07-27 01:37:17
【问题描述】:

如果条件为真,我正在尝试使 int 等于某个值。 我无法从 if 语句中获取血型 int,因此它可以应用于我的班级。我知道这可能是一个简单的解决方案,但我的大脑被炸了。

 private void btnAddPatient_Click(object sender, RoutedEventArgs e)////Add Patients
    {
        string name = txtPatientName.Text;
        int bloodType,age=30;           
        DateTime dob;
        bool bloodA = rbA.IsChecked.Equals(true);
        bool bloodB = rbB.IsChecked.Equals(true);
        bool bloodAB = rbAB.IsChecked.Equals(true);
        bool blood0 = rb0.IsChecked.Equals(true);




        // if (dpDOB.SelectedDate == null || txtPatientName.Text == ""||bloodType==0)
        if (dpDOB.SelectedDate == null || txtPatientName.Text == "" || !bloodA || !bloodAB || !bloodB || !blood0)
        {

            if (txtPatientName.Text == "")
            {
                MessageBox.Show("Please enter Patient's Name");
            }

            else if (!bloodA || !bloodAB || !bloodB || !blood0)
            {
                MessageBox.Show("Please enter patient's blood type");
            }

            //else if (dpDOB.SelectedDate == null)
            //{
            //    MessageBox.Show("Please select a date");
            //}

        }

        else
       if (bloodA)
        {
            bloodType = 0;

        }
        else if (bloodB)
        {
            bloodType = 1;
        }
        else if (bloodAB)
        {
            bloodType = 2;
        }
        else {             
            bloodType = 3;           

            dob = dpDOB.SelectedDate.Value;

            Patient patient= new Patient(name, age, bloodType);///cant get bloodtype value

            MainWindow mainWindow = Owner as MainWindow;

           patients.Add(patient);
            lstPatients.ItemsSource = null;
            lstPatients.ItemsSource = patients;
            // this.Close();
        }

【问题讨论】:

  • 当它在方法中时,它是方法中的局部变量。将其声明为方法之外的字段或属性,它将可供类的其余部分使用
  • 如果 bloodType 等于 3,您只是在创建并添加一个患者到 patients。最后一个 else 语句应该只包含 bloodType = 3

标签: c# wpf xaml if-statement boolean


【解决方案1】:

只有在所有其他条件都失败的情况下才会评估您想要血型的位置,因为您使用了 if else if 结构。此外,您在将其传递给 Patient 的构造函数之前将其分配给 3。因此,在评估此代码时,bloodType 将等于 3。

【讨论】:

    【解决方案2】:

    试试类似的东西

    int bloodtype = -1;
    

    (或其他您不会使用的其他值)。该变量仅在 if else 语句中设置,因此您不能将其发送到您的 Patient 类,因为它不等于条件之外的任何内容。

    【讨论】:

      【解决方案3】:

      您需要反转您的血型条件,并将您的患者创建从血型 3 检查块中取出:

      private void btnAddPatient_Click(object sender, RoutedEventArgs e)////Add Patients
      {
          string name = txtPatientName.Text;
          int bloodType,age=30;           
          DateTime dob;
          bool bloodA = rbA.IsChecked.Equals(true);
          bool bloodB = rbB.IsChecked.Equals(true);
          bool bloodAB = rbAB.IsChecked.Equals(true);
          bool blood0 = rb0.IsChecked.Equals(true);
      
      
          var bloodTypeDefined = bloodA || bloodAB || bloodB || blood0;
      
          // if (dpDOB.SelectedDate == null || txtPatientName.Text == ""||bloodType==0)
          if (dpDOB.SelectedDate == null || txtPatientName.Text == "" || !bloodTypeDefined)
          {
      
              if (txtPatientName.Text == "")
              {
                  MessageBox.Show("Please enter Patient's Name");
              }
      
              else if (!bloodTypeDefined)
              {
                  MessageBox.Show("Please enter patient's blood type");
              }
      
              //else if (dpDOB.SelectedDate == null)
              //{
              //    MessageBox.Show("Please select a date");
              //}
      
          }
      
          else
          {
             if (bloodA) bloodType = 0;
             else if (bloodB) bloodType = 1;
              else if (bloodAB) bloodType = 2;
              else bloodType = 3;           
      
              dob = dpDOB.SelectedDate.Value;
      
              Patient patient= new Patient(name, age, bloodType);///cant get bloodtype value
      
              MainWindow mainWindow = Owner as MainWindow;
      
             patients.Add(patient);
              lstPatients.ItemsSource = null;
              lstPatients.ItemsSource = patients;         
      
          }
      
      
              // this.Close();
          }
      

      【讨论】:

        猜你喜欢
        • 2013-07-05
        • 1970-01-01
        • 2018-05-07
        • 2021-08-31
        • 2015-07-28
        • 2015-12-14
        • 1970-01-01
        • 1970-01-01
        • 2018-04-18
        相关资源
        最近更新 更多