C#

时间:2017-09-24 14:06:26

标签: c#

我在C#中写了一个简单的程序,当你给出一个X和Y(X ^ Y)时,它会计算一个数字的幂。答案显示在文本框中。例如:X = 2且Y = 5,文本框中的答案是:

2 X 2 = 32
2 X 2 = 32
2 X 2 = 32
2 X 2 = 32
2 X 2 = 32
2 X 2 = 32

我的问题是如何才能得到这个结果:

2 X 2 = 2
2 X 2 X 2 = 8
2 X 2 X 2 X 2 = 16 
2 X 2 X 2 X 2 X 2 = 32

这是我的代码:

private void button1_Click (object sender, EventArgs e)
{
    double number1;
    doubLe number2;
    int count = 0;

    double power;
    number1 = double.Parse(Txtnumber1.Text};
    number2 = double.Parse(Txtnumber2.Text};

    while (count < number2)
    {
        power = Math.Pow(number1, number2);
        Txtanswer.Text = Txtanswer.Text + number1.ToString() + " " + " x" + " " + number1.ToString() + " " + "=" + power.ToString() + "\r\n";
        count += 1;
    }
}

Number1是X。

Number2是Y

1 个答案:

答案 0 :(得分:0)

需要很多改进,但有类似的东西吗?

class PowBuilder
{
    public static string PrintNumber(double number, double times)
    {
        string temp = "";
        for(int i = 1;i < times; i++)
        {
            temp += number.ToString() + " x ";
        }
        return temp;
    }

}
class Program
{
    private static string PowFan(double a, double b)
    {

        int count = 1;
        double power;
        string sb = "";

        while (count < b)
        {
            power = Math.Pow(a, count);
            sb += PowBuilder.PrintNumber(a, count) + a.ToString() + " = " + power.ToString() + " \r\n";
            count += 1;
        }
        return sb;
    }
        static void Main(string[] args)
    {
        Console.WriteLine(PowFan(2, 15).ToString());
    }
}

//output
//2 = 2
//2 x 2 = 4
//2 x 2 x 2 = 8
//2 x 2 x 2 x 2 = 16
//2 x 2 x 2 x 2 x 2 = 32
//2 x 2 x 2 x 2 x 2 x 2 = 64
//2 x 2 x 2 x 2 x 2 x 2 x 2 = 128
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 256
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 512
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 1024
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 2048
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 4096
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 8192
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 16384