为什么我的字符串比较返回意外结果?(Why my string comparison returns an unexpected result?)

编程入门 行业动态 更新时间:2024-10-26 15:13:39
为什么我的字符串比较返回意外结果?(Why my string comparison returns an unexpected result?)

我试图创建一个添加按钮和一些信息文本框的表单。 单击“添加”按钮时,应读取文本框中给出的所有信息,并在列表视图和树视图中显示。 这是我的添加按钮单击事件代码。

private void btnAdd_Click(object sender, EventArgs e) { if(txtBYear.Text == "" || txtGender.Text == "" || txtMSSV.Text == "" || txtName.Text=="" || cBoxClasses.Text == "") { MessageBox.Show("Missing information!", "Error"); return; } if (txtGender.Text !="Female" || txtGender.Text!="Male") { MessageBox.Show("Male or Female only!", "Error"); return; } var mssv = txtMSSV.Text; var name = txtName.Text; uint bYear; bool gender; var addr = txtAddress.Text; uint cl = (uint)cBoxClasses.SelectedValue; var clName = cBoxClasses.SelectedText; if (txtGender.Text == "Female") gender = true; else gender = false; if (uint.TryParse(txtBYear.Text, out bYear)) { MessageBox.Show("Incorrect Birth Year!", "Error"); return; } var newStudent = new Student(cl, mssv, name, bYear, gender, addr); Classes x; if(classes.GetClassesById(cl, out x)) { x.AttendingStudents.Add(newStudent); } else { var tmp = new Classes(cl, clName); classes.Add(tmp); tmp.AttendingStudents.Add(newStudent); } lViewMain.Update(); tViewMain.Update(); }

我尝试在txtGender文本框中键入“女性”并执行按钮单击,但是我的比较返回true ,这会使Messagebox弹出。

为什么我的检查textGender.Text != "Female" ||txtGender.Text != "Male"返回意想不到的价值?

I'm trying to create a form with an add button and some information text box. When I click the add button, all information given in text box should be read and show in list view and tree view. Here is my add button click event code.

private void btnAdd_Click(object sender, EventArgs e) { if(txtBYear.Text == "" || txtGender.Text == "" || txtMSSV.Text == "" || txtName.Text=="" || cBoxClasses.Text == "") { MessageBox.Show("Missing information!", "Error"); return; } if (txtGender.Text !="Female" || txtGender.Text!="Male") { MessageBox.Show("Male or Female only!", "Error"); return; } var mssv = txtMSSV.Text; var name = txtName.Text; uint bYear; bool gender; var addr = txtAddress.Text; uint cl = (uint)cBoxClasses.SelectedValue; var clName = cBoxClasses.SelectedText; if (txtGender.Text == "Female") gender = true; else gender = false; if (uint.TryParse(txtBYear.Text, out bYear)) { MessageBox.Show("Incorrect Birth Year!", "Error"); return; } var newStudent = new Student(cl, mssv, name, bYear, gender, addr); Classes x; if(classes.GetClassesById(cl, out x)) { x.AttendingStudents.Add(newStudent); } else { var tmp = new Classes(cl, clName); classes.Add(tmp); tmp.AttendingStudents.Add(newStudent); } lViewMain.Update(); tViewMain.Update(); }

I try type "Female" in txtGender text box and execute the button click but my comparison returns true which makes Messagebox pop up.

Why does my check textGender.Text != "Female" ||txtGender.Text != "Male" return that unexpected value?

最满意答案

代码执行您告诉它的操作。 您的条件textGender.Text != "Female" ||txtGender.Text != "Male"说“如果文本不是'女'或文本不是'男'。

既然它不是'男',它会进入if区块。 你需要的是&&操作符

请尝试使用以下条件

textGender.Text != "Female" && txtGender.Text != "Male"

The code does what you told it to do. Your condition textGender.Text != "Female" ||txtGender.Text != "Male" says "if the text is not 'Female' or is the text is not 'Male'.

Since it is not 'Male', it gets in the if block. What you need is the && operator

Try using the following condition instead

textGender.Text != "Female" && txtGender.Text != "Male"

更多推荐

本文发布于:2023-07-31 01:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   意外   string   comparison   unexpected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!