为什么如果结果正确则告诉我那是错误的

时间:2019-09-24 01:37:14

标签: c#

该程序使用ran.next以十进制方式带出随机数以获取总和,并存储在变量 sum1a (对于第一个数字)和 sum2a (第二个数字)并打印在“ Suma1.Text”中;操作员需要在名为“ Resultado”的文本框中捕获结果,其名称如何为“ Resultado1”并存储在变量 result1 中,然后与 sum1a 之和进行比较和 sum2a 并存储在 total1 中,以便与 result1 进行比较,如果 total1 result1 相匹配在没有匹配项的情况下,必须打印“ Correcto”或“ Incorrecto”,但有时即使结果正确也说“ Incorrecto”。

[计算器与程序] [1]

namespace Sumas_punto_decimal_prueba
{
public partial class Form1 : Form
{
    double sum1a, sum2a;
    string num1a, num2a;
    double total1;
    double result1;      

    public Form1()
    {
        InitializeComponent();
    }

    private void Generar_Click(object sender, EventArgs e)
    {

        //LIMPIADOR
        foreach (Control ctrl in this.Controls)
        {
            if (ctrl is TextBox)
            {
                TextBox text = ctrl as TextBox;
                text.Clear();
            }
        }

        Random ran = new Random();
        //SUMA 1
        sum1a = ran.Next(100 , 10000) / 100.00;
        sum2a = ran.Next(100 , 10000) / 100.00;
        num1a = sum1a.ToString("##.#0");
        num2a = sum2a.ToString("##.#0");

        if (sum1a < 10 && sum2a < 10)
        {
            Suma1.Text = "   0" + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 1 && sum2a < 1)
        {

            Suma1.Text = "  00" + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 10 && sum2a < 1)
        {
            Suma1.Text = "   0" + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 1 && sum2a < 10)
        {
            Suma1.Text = "  00" + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 10)
        {
            Suma1.Text = "   0" + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum1a < 1)
        {
            Suma1.Text = "  00" + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum2a < 10)
        {
            Suma1.Text = "   " + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum2a < 1)
        {
            Suma1.Text = "   " + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else if (sum2a < 1)
        {
            Suma1.Text = "   " + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
        else
        {
            Suma1.Text = "   " + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
        }
    }

    private void Calificar_Click(object sender, EventArgs e)
    {
        //SUMA NUMERO 1
        double.TryParse(Resultado1.Text, out result1);
        total1 = sum1a + sum2a;
        if (result1 == total1)
        {
            Validacion1.Text = "Correcto";
        }
        else
        {
            Validacion1.Text = "Incorrecto. \r\nEl resultado es: " + total1;
        }
    }

}

}

有时会发生错误的原因可能是什么?我受到刺激,发生了15次之一。

感谢所有人的帮助,但已经使用Math.Round()和“零占位符”找到了解决方案

        sum1a = ran.Next(10000) / 100.00;
        sum2a = ran.Next(10000) / 100.00;
        sum1a = Math.Round(sum1a, 2, MidpointRounding.AwayFromZero);
        sum2a = Math.Round(sum2a, 2, MidpointRounding.AwayFromZero);
        num1a = sum1a.ToString("00.00");
        num2a = sum2a.ToString("00.00");

            Suma1.Text = "   " + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";

谢谢。 uwu

1 个答案:

答案 0 :(得分:0)

可能是因为它们根本不相等, 可能不是所有数字都被捕获了。比较 取而代之的是字符串,并使用打印操作来查看错误的出处

if (string == string)
{
// Ganastes!
}

if("159.73" == "159.73")
{
// Correct!
}