C# 为什么使用逻辑条件运算符的If语句&&;还有!=你给我的结果不对吗?
我是编程新手,正在学习C。我正在开发一个程序,对文本框中输入的文本进行加密和解密 我想确保当用户单击按钮加密或解密文本时,文本和密码文本框不是空的。所以,我使用的是逻辑条件运算符C# 为什么使用逻辑条件运算符的If语句&&;还有!=你给我的结果不对吗?,c#,conditional,operators,logical-operators,C#,Conditional,Operators,Logical Operators,我是编程新手,正在学习C。我正在开发一个程序,对文本框中输入的文本进行加密和解密 我想确保当用户单击按钮加密或解密文本时,文本和密码文本框不是空的。所以,我使用的是逻辑条件运算符&&和=在运行加密文本的代码之前评估文本框。当我将TextBox的text属性值与空字符串进行比较时,似乎得到了错误的结果 当我在文本框中没有任何数据的情况下单击“加密”或“解密”按钮时,语句:if(text!=”&&encryptPassword!=”)的行为就像每个测试都是真的一样,并运行加密或解密代码。我试过使用e
&&
和=
在运行加密文本的代码之前评估文本框。当我将TextBox的text属性值与空字符串进行比较时,似乎得到了错误的结果
当我在文本框中没有任何数据的情况下单击“加密”或“解密”按钮时,语句:if(text!=”&&encryptPassword!=”)
的行为就像每个测试都是真的一样,并运行加密或解密代码。我试过使用equals
,玩括号,颠倒顺序,但都没有用。请帮忙
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 CryptoMakerII
{
public partial class CryptoMakerII : Form
{
public CryptoMakerII()
{
InitializeComponent();
}
private void UpdateControls(string crypto)
{
if (crypto == "Encrypt")
{
lblEncryptPassword.Visible = false;
txtEncryptPassword.Visible = false;
btnEncrypt.Visible = false;
txtDecryptPassword.Visible = true;
btnDecrypt.Visible = true;
lblDecryptPassword.Visible = true;
lblText.Text = "Encrypted Text";
//txtDecryptPassword.Text = " ";
//txtEncryptPassword.Text = " ";
}
else
{
lblEncryptPassword.Visible = true;
txtEncryptPassword.Visible = true;
btnEncrypt.Visible = true;
txtDecryptPassword.Visible = false;
btnDecrypt.Visible = false;
lblDecryptPassword.Visible = false;
lblText.Text = "Text to Encrypt";
txtDecryptPassword.Text = " ";
txtEncryptPassword.Text = " ";
}
}
private void btnEncrypt_Click(object sender, EventArgs e)
{
DES_Crypto desCrypto = new DES_Crypto();
string text = txtText.Text;
string encryptPassword = txtEncryptPassword.Text;
if (text != " " && encryptPassword != " ")
{
string encryptedText = desCrypto.EncryptString(text, encryptPassword);
txtText.Text = encryptedText;
UpdateControls("Encrypt");
}
else
{
MessageBox.Show("Please enter text to encrypt and password");
}
}
private void btnDecrypt_Click(object sender, EventArgs e)
{
DES_Crypto desCrypto = new DES_Crypto();
if (txtText.Text != " " && txtDecryptPassword.Text != " ")
if (txtDecryptPassword.Text == txtEncryptPassword.Text)
{
string decryptedText = desCrypto.DecryptString(txtText.Text, txtDecryptPassword.Text);
txtText.Text = decryptedText;
UpdateControls("Decrypt");
}
else
{
MessageBox.Show("The password is incorrect!");
}
else
MessageBox.Show("Please enter password to decrypt");
}
}
}
您正在检查字符串是否正好等于1个空白字符,而不是检查它是否为空。C#有一个内置的方法来检查字符串是否为空:
string.IsNullOrEmpty(str)
所以不是
if (txtText.Text != " " && txtDecryptPassword.Text != " ")
试一试
您正在检查字符串是否正好等于1个空白字符,而不是检查它是否为空。C#有一个内置的方法来检查字符串是否为空:
string.IsNullOrEmpty(str)
所以不是
if (txtText.Text != " " && txtDecryptPassword.Text != " ")
试一试
你试过加密吗?没有,迈克尔。但是用户3144325看到了我的错误并纠正了我。我应该使用string.IsNullOrEmpty(string)方法。谢谢你试过加密吗?没有,迈克尔。但是用户3144325看到了我的错误并纠正了我。我应该使用string.IsNullOrEmpty(string)方法。谢谢