我需要一些有关如何验证if,else if语句的帮助. [英] I need some help about how I can validate my if , else if statements.

查看:113
本文介绍了我需要一些有关如何验证if,else if语句的帮助.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已解决.

可以吗?

This sh*t has been solved.

ok?

推荐答案

我可以看到您的if..else语句不正确.您应该使用> =& < =运算符两者.
试试这个:
As I can see your if..else statement is incorrect. You should validate it using >= & <= operators both.
Try this:
//Determine pay rate and tax rates based on entered data
if (annual_salary >= 0 && annual_salary <= 16500)
{
    tax_rate = 11.32F;
    pay_rate = 8.68F;
}



:
谢谢@@ Amitgajjar给我建议.
我执行了您的代码,它运行正常.要进行错误检查,请使用try..catch块.
使用这个:



:
Thank you @@Amitgajjar to suggest me.
Hi I executed your code it''s working perfectly. For error checking use try..catch blocks.
Use this:

//Determine pay rate and tax rates based on entered data
try{
    if (annual_salary >= 0 && annual_salary <= 16500)
    {
        tax_rate = 11.32F;
        pay_rate = 8.68F;
    }
}catch(Exception ex)
{
    Console.WriteLine(ex.Message);
}



请参阅链接以获取更多信息:
尝试捕获(C#参考) [ ^ ]
try-catch-finally(C#参考) [ ^ ]

--Amit



Refer the links for more information:
try-catch (C# Reference)[^]
try-catch-finally (C# Reference)[^]

--Amit


否.无论您的老师说什么,都是正确的.我建议您继续写您的原始文章.

当您依次使用它时,就像这样.
No. Whatever your teacher said, its right. I would suggest you to go ahead with your original post..

When you are having it in sequence, like this.
if (annual_salary >= 0 && annual_salary <= 16500)
            {
                tax_rate = 11.32F;
                pay_rate = 8.68F;
            }
            else if (annual_salary <= 19500)
            {
                tax_rate = 15.14F;
                pay_rate = 10.26F;
            }





相同




its same as

if (annual_salary >= 0 && annual_salary <= 16500)
{
    tax_rate = 11.32F;
    pay_rate = 8.68F;
}
else if (annual_salary >= 16500 && annual_salary <= 19500 )
{
    tax_rate = 15.14F;
    pay_rate = 10.26F;
}



例如,当我通过17000时,肯定会采用15.14F的税率,因为它将首先在if循环中失败,然后在if中进入else.

如果您想测试完整的代码,

使用以下值并运行一次并测试您的代码.

输入:年薪期望值:税率
14000 11.32F
17000 15.14F
21000 22.65F
32000 27.1F
34000 30.92F
45000 35.72F
78000 40.72F
91000 50.52F



For eg, when i pass 17000, definitely it will take tax rate as 15.14F, since it will fail first if loop and enters else if.

If you want to test the complete code,

use the following values and run once and test your code.

Input:Annual_salary Expected:Tax_rate
14000 11.32F
17000 15.14F
21000 22.65F
32000 27.1F
34000 30.92F
45000 35.72F
78000 40.72F
91000 50.52F




您需要在许多地方添加一些异常处理.

示例1:不应允许用户输入低于0的Annual_Salary.
所以你的代码就像,

Hi,

You need to add some exception handling at many places.

Example 1: User should not allowed to enter annual_salary below 0.
So your code would be like,

If(annual_salary<=0)
{
throw new ArgumentOutOfRangeException(); //// Here you can even prompt some message. if you are developing API then you should throw Exception to handled from client.
}



示例2:用户不应输入空名称.
代码就像,



Example 2: User should not enter Empty name.
the code would be like,

if (string.IsNullOrEmpty(full_name))
{
throw new ArgumentNullException(); //// Here you can even prompt to enter correct name.
}



这样,可以在您的应用程序中添加许多验证.

祝你好运

谢谢
-Amit Gajjar.



This way so many validation can be added in your application.

Best of luck

Thanks
-Amit Gajjar.


这篇关于我需要一些有关如何验证if,else if语句的帮助.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆