-1
我的代碼工作正常,但需要進行一些優化,而無需任何其他變量或循環。 輸出應爲:5 X 4 X 3 X 2 X 1 = 120VB.NET控制檯應用程序|用戶輸入數字的顯示因子
Private Sub FactorialCalculation()
Dim Number, Factorial As Integer
Console.WriteLine("Please enter a number")
Number = CInt(Console.ReadLine)
Factorial = 1
Console.WriteLine("Your number is " & Number)
If Number > 0 Then
Console.Write(Number & " X ")
For Count As Integer = 1 To Number
If (Number - Count) > 0 And (Number - Count) <> 1 Then
Factorial *= Count
Console.Write("{0} X ", Number - Count)
ElseIf (Number - Count) = 0 Then
Factorial *= Count
ElseIf (Number - Count) = 1 Then
Factorial *= Count
Console.Write(Number - Count)
End If
Next
Console.WriteLine(" = {0}", Factorial)
Else
Console.WriteLine("Please enter a positive number greater than 0")
End If
End Sub
也許這個問題會更好http://codereview.stackexchange.com/? – Werdna