Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的代码中有错误,它只读取第一个if语句,而else if不读取?_C#_If Statement - Fatal编程技术网

C# 我的代码中有错误,它只读取第一个if语句,而else if不读取?

C# 我的代码中有错误,它只读取第一个if语句,而else if不读取?,c#,if-statement,C#,If Statement,这是C#程序我在这行代码中遇到了一个问题: 我输入的数字大于或等于它运行的400 我输入的数字小于或等于它运行的500 我输入400到500之间的数字,比如401到499,它运行 I空输入=erorr 我输入字母=erorr 请注意,我运行的代码只接受它单独运行的整数 而且它也运行不接受空输入的代码 你认为哪里不对 int parsedValue; if (int.Parse(txtVacate.Text) <= 400 || int.Parse(txtVa

这是C#程序我在这行代码中遇到了一个问题:

我输入的数字大于或等于它运行的400 我输入的数字小于或等于它运行的500 我输入400到500之间的数字,比如401到499,它运行 I空输入=erorr 我输入字母=erorr

请注意,我运行的代码只接受它单独运行的整数 而且它也运行不接受空输入的代码

你认为哪里不对

        int parsedValue;
        if (int.Parse(txtVacate.Text) <= 400 || int.Parse(txtVacate.Text) >= 500)
            MessageBox.Show("Romms provided is not vacant or does not exist at all.");

        else if (txtVacate.Text == " ")
            MessageBox.Show("You provide empty");

        else if (!int.TryParse(txtVacate.Text, out parsedValue))
            MessageBox.Show("Please provide right info");
        else
        {
            MySqlConnection connection = null;
            string hostname = "localhost";
            string database = "aparece_hoteldb";
            string username = "root";
            string password = "";
            connection = new MySqlConnection("host=" + hostname +
                                            ";database=" + database +
                                            ";username=" + username +
                                            ";password=" + password + ";");


            string table = "reservations";
            string query = "DELETE FROM reservations WHERE RoomNumber = " + txtVacate.Text;
            connection.Open();
            MySqlDataAdapter da_res = null;
            DataSet ds_res = null;
            ds_res = new DataSet();
            da_res = new MySqlDataAdapter(query, connection);
            da_res.Fill(ds_res, table);
            MessageBox.Show("Room" + " " + txtVacate.Text + " " + "is now Vacant please reload!");
            dataGridView2.DataSource = ds_res.Tables[table];
            this.Close();
int-parsedValue;
if(int.Parse(txtVacation.Text)=500)
MessageBox.Show(“提供的ROM不是空的或根本不存在。”);
else if(txtVacation.Text==“”)
MessageBox.Show(“您提供的是空的”);
如果(!int.TryParse(txtvate.Text,out parsedValue))为else
MessageBox.Show(“请提供正确的信息”);
其他的
{
MySqlConnection=null;
字符串hostname=“localhost”;
string database=“aparece\u hoteldb”;
字符串username=“root”;
字符串密码=”;
连接=新的MySqlConnection(“主机=”+主机名+
“数据库=”+数据库+
“用户名=”+用户名+
“password=“+password+”;”;
string table=“预订”;
string query=“从RoomNumber=“+txtVacation.Text”处的预订中删除;
connection.Open();
MySqlDataAdapter da_res=null;
数据集ds_res=null;
ds_res=新数据集();
da_res=新的MySqlDataAdapter(查询、连接);
数据存储填充(数据存储,表格);
MessageBox.Show(“房间“+”+txtVacation.Text++”现在空了,请重新加载!”);
dataGridView2.DataSource=ds_res.Tables[table];
这个。关闭();

恐怕您的逻辑是错误的。如果
txtVate.Text
不包含整数,您的代码将爆炸。这是评估不同条件的正确顺序

int parsedValue = 0;
bool isValid = int.TryParse(txtVacate.Text, out parsedValue));
if (txtVacate.Text == " ") // Better: if (txtVacate.Text.Trim() == string.Empty)
{
    MessageBox.Show("You provide empty");
}
else if (!isValid)
{
    MessageBox.Show("Please provide right info");
}
else if (parsedValue <= 400 || parsedValue >= 500)
{
    MessageBox.Show("Romms provided is not vacant or does not exist at all.");   
}
else
{
    ...
}
int-parsedValue=0;
bool isValid=int.TryParse(txtwavate.Text,out parsedValue));
if(txtwavate.Text==“”)//更好:if(txtwavate.Text.Trim()==string.Empty)
{
MessageBox.Show(“您提供的是空的”);
}
如果(!isValid),则为else
{
MessageBox.Show(“请提供正确的信息”);
}
else if(parsedValue=500)
{
MessageBox.Show(“提供的ROM不是空的或根本不存在。”);
}
其他的
{
...
}

恐怕您的逻辑是错误的。如果
txtVate.Text
不包含整数,您的代码将爆炸。这是评估不同条件的正确顺序

int parsedValue = 0;
bool isValid = int.TryParse(txtVacate.Text, out parsedValue));
if (txtVacate.Text == " ") // Better: if (txtVacate.Text.Trim() == string.Empty)
{
    MessageBox.Show("You provide empty");
}
else if (!isValid)
{
    MessageBox.Show("Please provide right info");
}
else if (parsedValue <= 400 || parsedValue >= 500)
{
    MessageBox.Show("Romms provided is not vacant or does not exist at all.");   
}
else
{
    ...
}
int-parsedValue=0;
bool isValid=int.TryParse(txtwavate.Text,out parsedValue));
if(txtwavate.Text==“”)//更好:if(txtwavate.Text.Trim()==string.Empty)
{
MessageBox.Show(“您提供的是空的”);
}
如果(!isValid),则为else
{
MessageBox.Show(“请提供正确的信息”);
}
else if(parsedValue=500)
{
MessageBox.Show(“提供的ROM不是空的或根本不存在。”);
}
其他的
{
...
}

您的第一条if语句使用

int.Parse(txtVacate.Text)
对于空值或非数字(文本)值,这将引发FormatException,因此您的“else-if”案例将永远不会被计算


您应该使用TryParse,而不是在第一个“if”中解析,或者捕获FormatException并相应地处理它。

您的第一个if语句使用

int.Parse(txtVacate.Text)
对于空值或非数字(文本)值,这将引发FormatException,因此您的“else-if”案例将永远不会被计算


您应该使用TryParse,而不是在第一个“if”中进行解析,或者捕获FormatException并相应地处理它。

您可以通过几个步骤简化它

首先,检查以确保输入不为null、空格或空

如果有一个值,则对其进行分析以确保它是一个数值

if (!string.IsNullOrWhiteSpace(txtVacate.Text))
{
    in parsedValue = 0;
    bool isValid = int.TryParse(txtVacate.Text, out parsedValue));

    if (isValid) 
    {
        if (parsedValue <= 400 || parsedValue >= 500)
            MessageBox.Show("Romms provided is not vacant or does not exist at all.");
        else 
        {
            // main stuff here
        }
    }
    else 
    {
        MessageBox.Show("Please provide right info");
    }
else
{
    MessageBox.Show("You provide empty");
}
if(!string.IsNullOrWhiteSpace(txtwavate.Text))
{
在parsedValue中=0;
bool isValid=int.TryParse(txtwavate.Text,out parsedValue));
如果(有效)
{
if(parsedValue=500)
MessageBox.Show(“提供的ROM不是空的或根本不存在。”);
其他的
{
//这里的主要内容
}
}
其他的
{
MessageBox.Show(“请提供正确的信息”);
}
其他的
{
MessageBox.Show(“您提供的是空的”);
}

虽然这很复杂,但应该正确地处理所有用例

您可以通过几个步骤简化它

首先,检查以确保输入不为null、空格或空

如果有一个值,则对其进行分析以确保它是一个数值

if (!string.IsNullOrWhiteSpace(txtVacate.Text))
{
    in parsedValue = 0;
    bool isValid = int.TryParse(txtVacate.Text, out parsedValue));

    if (isValid) 
    {
        if (parsedValue <= 400 || parsedValue >= 500)
            MessageBox.Show("Romms provided is not vacant or does not exist at all.");
        else 
        {
            // main stuff here
        }
    }
    else 
    {
        MessageBox.Show("Please provide right info");
    }
else
{
    MessageBox.Show("You provide empty");
}
if(!string.IsNullOrWhiteSpace(txtwavate.Text))
{
在parsedValue中=0;
bool isValid=int.TryParse(txtwavate.Text,out parsedValue));
如果(有效)
{
if(parsedValue=500)
MessageBox.Show(“提供的ROM不是空的或根本不存在。”);
其他的
{
//这里的主要内容
}
}
其他的
{
MessageBox.Show(“请提供正确的信息”);
}
其他的
{
MessageBox.Show(“您提供的是空的”);
}
虽然很复杂,但这应该能正确处理所有用例

我是C#编程语言新手!我是C#编程语言新手!