如果语句失败c#

时间:2014-03-16 16:05:03

标签: c#

我有以下代码,如果信用> = 7且Salary> = 20000则返回Qualify(),否则返回NoQualify,但由于某种原因它只返回Qualify = / 任何帮助将不胜感激!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LabAssignFiveLoanThingy   
{
    public class Loan
    {
        public static void Qualify()
        {
           Console.WriteLine("Sorry, at this time you do not meet the requirements for the loan.");
        }

        public static void NoQualify()
        {
            Console.WriteLine("Congratulations! You meet the requirements!");
        }

    }

    public class Program
    {
        static void Main(string[] args)
        {
            double Salary;
            int credit;
            Console.WriteLine("Hello, please enter your yearly salary:");
            Salary = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Awesome! please enter your credit rating, on a scale of 1-10:");
            credit = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You've entered the following:");
            Console.WriteLine("Salary: {0}\nCredit Rating: {1}", Salary, credit);
            if (Salary < 20000 || credit < 7)      
                Loan.NoQualify();

            if (Salary >= 20000 && credit >= 7)
                Loan.Qualify();

            Console.ReadKey();
        }

    }
}

4 个答案:

答案 0 :(得分:3)

这里的主要问题是Qualify()NoQualify()方法中的消息是交换的。

他们需要像这样:

public static void Qualify()
{
    Console.WriteLine("Congratulations! You meet the requirements!");
}

public static void NoQualify()
{
    Console.WriteLine("Sorry, at this time you do not meet the requirements for the loan.");
}

第二个问题是if-if控制流结构并不是你想要的。虽然它不会改变代码的结果,但它使得代码在您尝试使用if-else控制流结构时更具可读性。

您正在尝试设计一个不允许这两个if语句评估为真的系统 - 那么为什么不这样做才能永远不会发生?

if (Salary >= 20000 && credit >= 7)
{
    Loan.Qualify();
}
else
{
    Loan.NoQualify();
}

答案 1 :(得分:2)

  

如果信用&gt; = 7且Salary&gt; = 20000,否则返回NoQualify

您似乎需要if-else声明:

if (Salary >= 20000 && credit >= 7)
    Loan.Qualify();
else
    Loan.NoQualify();

答案 2 :(得分:2)

您已更换了Qualify和NoQualify消息!

答案 3 :(得分:1)

public static void Qualify()
{
    Console.WriteLine("Congratulations! You meet the requirements!");
}

public static void NoQualify()
{
    Console.WriteLine("Sorry, at this time you do not meet the requirements for the loan.");
}