Home > other >  Why does it Calculate some numbers right and some numbers wrong? C#
Why does it Calculate some numbers right and some numbers wrong? C#

Time:10-14

my calculator has a error i guess, sometimes it calculates right, sometimes wrong. I wanna make an U-R-I Calculator and started with U = R * I.

wrong calc:

Right calc:

im pretty new to c# and started after 3 months of console programming to wpf. here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace URI_CALC
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    double test_1;
    string s_test1;
    double test_2;
    string s_test2;
    double result;
    string s_result;

    private void textbox_test_TextChanged(object sender, TextChangedEventArgs e)
    {
        s_test1 = textbox_test.Text;
    }

    private void Button_Calc_GETU_Click(object sender, RoutedEventArgs e)
    {
        test_1 = Convert.ToDouble(s_test1);
        test_2 = Convert.ToDouble(s_test2);
        result = (test_1 * test_2);
        s_result = Convert.ToString(result);
        textblock_u.Text = s_result;
    }

    private void TestTextbox2_TextChanged(object sender, TextChangedEventArgs e)
    {
        s_test2 = textbox_test.Text;
    }



   }
   }

CodePudding user response:

you have a bug, this two are the same

private void textbox_test_TextChanged(object sender, TextChangedEventArgs e)
{
    s_test1 = textbox_test.Text;
}

  
private void TestTextbox2_TextChanged(object sender, TextChangedEventArgs e)
{
    s_test2 = textbox_test.Text;
}

fix the names of text boxes, they should be different

  • Related