c# SIMPLE factorial program

僤鯓⒐⒋嵵緔 提交于 2019-12-25 11:09:52

问题


private void button1_Click(object sender, EventArgs e)
{
        String UserNumber = this.textBox1.Text;
        int NewUserNumber = Convert.ToInt32(UserNumber);
        int result = 0;
        int second = 0;

        while (NewUserNumber >= 1) 
        {

            result = NewUserNumber * (NewUserNumber - 1);
            NewUserNumber--;
        }
        String i = Convert.ToString(result);
        this.textBox2.Text = i;
    }
}

While I understand this is homework for me, I am stuck. I really really don't want this solved, I want to do it myself.

I don't understand why it's not working.. It's outputting 2 no matter what I put in.

I could do this in Java easily but the converting gets to me..

Any help would be great.


回答1:


Your problem is not in the conversion. Please look at

result = NewUserNumber * (NewUserNumber - 1);



回答2:


            while (NewUserNumber > 1)
            {
                result = result * NewUserNumber;
                NewUserNumber--;
            }



回答3:


Initialize result as

int Result=1;

And write for loop as follows

while (NewUserNumber >= 1) 
{
   result = result * NewUserNumber;
   NewUserNumber--;
}

OR

you can even use follwing

public int Factorial(int num)
{
  if(num==1)
     return 1;
  return num * Factorial(num-1);
}



回答4:


You need to look at your while loop structure and how you are setting the result value outside it and the multiplication

    while (NewUserNumber >= 1) 
    {

        result = NewUserNumber * (NewUserNumber - 1);
        NewUserNumber--;
    }
    String i = Convert.ToString(result);

Also you have declared another int second = 0; - dont see why you are declaring that.




回答5:


My advice is to try running over the code manually with different inputs.

You should run over the loop and try to figure what is the value of result and NewUserNumber is in each iteration.

Good luck...




回答6:


You should try to run a debugger on your code then step through using the watch window. This is easy to do and will show you exactly what each value is at each point (you set break points). What IDE are you using?




回答7:


int Factorial(int input) { int answer = 0;

if (input > 0)
{
    count = 1;
    while (count <= input)
    {
        if (count == 1)
        {
            answer= 1;
            count++;
        }
        else
        {
            answer = count * answer;
            count++;
        }
    }
}
else
{
    MessageBox.Show("Please enter only a positive integer.");
}

return answer;

}




回答8:


    static void Main(string[] args)
    {
        int number, fact;
        Console.WriteLine("enter the number for geting factorial");
        number = Convert.ToInt32(Console.ReadLine());
        fact = number;
        for (int i = fact - 1; i > 0; i--)
        {
            fact = fact * i;
        }
        Console.WriteLine(fact);
        Console.ReadLine();
    }


来源:https://stackoverflow.com/questions/17966221/c-sharp-simple-factorial-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!