저는 숫자를 반복하고 구의 부피를 계산하려고합니다.
사용자는 숫자를 입력 한 다음 사용자의 숫자에 도달 할 때까지 숫자 볼륨을 반복합니다.
그러나 루프는 첫 번째 숫자 계산을 건너 뜁니다.
내 현재 코드는 다음과 같습니다.
public static float Calculation(float i)
{
//Calculation
float result = (float)(4 * Math.PI * Math.Pow(i, 3) / 3);
//Return the result
return result;
}
static void Main(string[] args)
{
//Declare the result variable in the main method
float result = 0;
//Ask the user to input a number
Console.WriteLine("Please input a number:");
int radius = int.Parse(Console.ReadLine());
//For loop, that runs until i is lesser than or equals to the radius that the user input
for(int i = 0; i <= radius; i++)
{
Console.WriteLine($"The Sphere's volume with radius {i} is {result}\n");
//Setting the result by calling the Calculation method and setting the radius to the current i value in the loop
result = Calculation(i);
}
Console.ReadLine();
}
출력은 다음과 같습니다.
The Sphere's volume with radius 0 is 0
The Sphere's volume with radius 1 is 0
The Sphere's volume with radius 2 is 4,1887903
The Sphere's volume with radius 3 is 33,510323
The Sphere's volume with radius 4 is 113,097336
The Sphere's volume with radius 5 is 268,08258
The Sphere's volume with radius 6 is 523,59875
The Sphere's volume with radius 7 is 904,7787
The Sphere's volume with radius 8 is 1436,755
The Sphere's volume with radius 9 is 2144,6606
The Sphere's volume with radius 10 is 3053,6282
for 루프를 다음과 같이 변경하십시오.
//For loop, that runs until i is lesser than or equals to the radius that the user input
for(int i = 0; i <= radius; i++)
{
//Setting the result by calling the Calculation method and setting the radius to the current i value in the loop
result = Calculation(i);
Console.WriteLine($"The Sphere's volume with radius {i} is {result}\n");
}
먼저 결과를 계산 한 다음 인쇄 할 수 있습니다.
감사,
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다