gpt4 book ai didi

c# - 从 If 语句返回 Int

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:50 24 4
gpt4 key购买 nike

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

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

最佳答案

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

关于c# - 从 If 语句返回 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42638241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com