如何将其转换为字符串?

时间:2017-03-27 13:36:46

标签: c#

我正在尝试制作一个简单的可视化计算器,如果你按下它,它当前只是添加数字,这意味着1 + 1是2.我希望它是11.我该怎么做?在我做完之后,我的代码将如何看待?

{
    double value1 = 0;
    double value2 = 0;
    double finalResult = 0;
    double output = 0;
    bool second = false;
    bool plus = false;
    bool minus = false;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (second == false)
        {
            value1 = value1 + 1;
            output = value1 * 10 + 1;
            textresult.Text = output.ToString();
        }
        else if (second == true)
        {
            value2 = value2 + 1;
            output = value2;
            textresult.Text = output.ToString();
        }

    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        if (second == false)
        {
            value1 = value1 + 2;
            output = value1;
            textresult.Text = output.ToString();
        }
        else if (second == true)
        {
            value2 = value2 + 2;
            output = value2;
            textresult.Text = output.ToString();
        }
    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        if (second == false)
        {
            value1 = value1 + 3;
            output = value1;
            textresult.Text = output.ToString();
        }
        else if (second == true)
        {
            value2 = value2 + 3;
            output = value2;
            textresult.Text = output.ToString();
        }
    }

    private void button4_Click(object sender, RoutedEventArgs e)
    {
        if (second == false)
        {
            value1 = value1 + 4;
            output = value1;
            textresult.Text = output.ToString();
        }
        else if (second == true)
        {
            value2 = value2 + 4;
            output = value2;
            textresult.Text = output.ToString();
        }
    }

    private void button5_Click(object sender, RoutedEventArgs e)
    {
        if (second == false)
        {
            value1 = value1 + 5;
            output = value1;
            textresult.Text = output.ToString();
        }
        else if (second == true)
        {
            value2 = value2 + 5;
            output = value2;
            textresult.Text = output.ToString();
        }
    }

    private void button6_Click(object sender, RoutedEventArgs e)
    {
        if (second == false)
        {
            value1 = value1 + 6;
            output = value1;
            textresult.Text = output.ToString();
        }
        else if (second == true)
        {
            value2 = value2 + 6;
            output = value2;
            textresult.Text = output.ToString();
        }
    }

    private void button7_Click(object sender, RoutedEventArgs e)
    {
        if (second == false)
        {
            value1 = value1 + 7;
            output = value1;
            textresult.Text = output.ToString();
        }
        else if (second == true)
        {
            value2 = value2 + 7;
            output = value2;
            textresult.Text = output.ToString();
        }
    }

    private void button8_Click(object sender, RoutedEventArgs e)
    {
        if (second == false)
        {
            value1 = value1 + 8;
            output = value1;
            textresult.Text = output.ToString();
        }
        else if (second == true)
        {
            value2 = value2 + 8;
            output = value2;
            textresult.Text = output.ToString();
        }
    }

    private void button9_Click(object sender, RoutedEventArgs e)
    {
        if (second == false)
        {
            value1 = value1 + 9;
            output = value1;
            textresult.Text = output.ToString();
        }
        else if (second == true)
        {
            value2 = value2 + 9;
            output = value2;
            textresult.Text = output.ToString();
        }
    }

    private void buttonClear_Click(object sender, RoutedEventArgs e)
    {
        //remember to add all the other values :)
        value1 = 0;
        value2 = 0;
        finalResult = 0;
        output = 0;
        second = false;
        plus = false;
        minus = false;
        textresult.Text = output.ToString();
    }

    private void buttonPlus_Click(object sender, RoutedEventArgs e)
    {
        output = 0;
        textresult.Text = output.ToString();
        second = true;
        plus = true;
    }

    private void buttonMinus_Click(object sender, RoutedEventArgs e)
    {
        output = 0;
        textresult.Text = output.ToString();
        second = true;
        minus = true;
    }

    private void buttonIs_Click(object sender, RoutedEventArgs e)
    {
        if (plus == true)
        {
            finalResult = value1 + value2;
            output = finalResult;
            textresult.Text = output.ToString();
        }
        else if (minus == true)
        {
            finalResult = value1 - value2;
            output = finalResult;
            textresult.Text = output.ToString();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你可以用这个转换int - string。

int i = 1;
string s = Convert.ToString(i);

答案 1 :(得分:0)

只是想分享一些想法/建议以简化您的代码

enum Operators
{
    NewOperation = -1,
    NotSet = 0,
    Plus = 1,
    Minus = 2
    //...
};

int value1, value2;
const string RESET_VALUE = "0";
Operators operation;

public MainWindow() { InitializeComponent(); }

private void MainWindow_Load(object sender, EventArgs e) { textresult.Text = RESET_VALUE; }
private void button1_Click(object sender, EventArgs e) { AppendNumber(1); }
private void button2_Click(object sender, EventArgs e) { AppendNumber(2); }
private void button3_Click(object sender, EventArgs e) { AppendNumber(3); }
private void button4_Click(object sender, EventArgs e) { AppendNumber(4); }
private void button5_Click(object sender, EventArgs e) { AppendNumber(5); }
private void button6_Click(object sender, EventArgs e) { AppendNumber(6); }
private void button7_Click(object sender, EventArgs e) { AppendNumber(7); }
private void button8_Click(object sender, EventArgs e) { AppendNumber(8); }
private void button9_Click(object sender, EventArgs e) { AppendNumber(9); }
private void button0_Click(object sender, EventArgs e) { AppendNumber(0); }

private void AppendNumber(int selected_number)
{
    if (operation == Operators.NewOperation)
    {
        operation = Operators.NotSet;
        textresult.Text = $"{selected_number}";         
    }
    else if (textresult.Text.StartsWith("0")) 
    { 
        textresult.Text = $"{selected_number}"; 
    }
    else 
    { 
        textresult.Text = $"{textresult.Text}{selected_number}";
    }

    if (operation == Operators.NotSet)
        value1 = int.Parse(textresult.Text);
    else
        value2 = int.Parse(textresult.Text);
}
private void buttonPlus_Click(object sender, EventArgs e)
{
    operation = Operators.Plus;
    textresult.Text = RESET_VALUE;
}
private void buttonMinus_Click(object sender, EventArgs e)
{
    operation = Operators.Minus;
    textresult.Text = RESET_VALUE;
}
private void buttonIs_Click(object sender, EventArgs e)
{
    switch (operation)
    {
        case Operators.Minus: textresult.Text = $"{(value1 - value2)}"; break;
        case Operators.Plus: textresult.Text = $"{(value1 + value2)}"; break;
    }

    operation = Operators.NewOperation;
    value1 = int.Parse(textresult.Text); //save result for next operation
    value2 = 0;
}
private void buttonClear_Click(object sender, EventArgs e)
{
    operation = Operators.NotSet;
    textresult.Text = RESET_VALUE;
    value1 = value2 = 0;
}