2016-11-06 3 views
1
/* Write a program that asks the user 
* to enter the starting point and end 
* point of the counting range and the 
* increment value and displays the total 
* of the numbers within that range 
*/ 

int start; 
int end; 
int increment; 
int sum = 0; 
int count= 0; 

Console.WriteLine(" Enter the start number "); 
start = Int32.Parse(Console.ReadLine()); 

Console.WriteLine(" Enter the end number "); 
end = Int32.Parse(Console.ReadLine()); 

Console.WriteLine(" Enter the increment number "); 
increment = Int32.Parse(Console.ReadLine()); 

for (start = ; end <= start ; count = count + increment ) 
{ 

    Console.WriteLine(" Number is: " + count); 

} 

Console.WriteLine(" Sum is: " + sum); 
Console.ReadKey(); 
+0

가 루프 – yashpandey

+2

잘못 귀하의 코드를 약간 수정 다만 숙제와 일부 코드. 당신이 갇혀있는 것에 대해 명확히하고, 예상되는 결과와 실제 결과, 오류 메시지 등을 명확히하십시오. –

+0

2 일 안에 루프를 테스트하기위한 준비를하고 있습니다. 이것은 샘플 질문입니다. 이것은 내가 돌아 오는 것입니다. 시작 번호를 입력하십시오. 1 종료 번호를 입력하십시오. 10 증분 번호를 입력하십시오. 2 숫자 : 0 합계 : 0 이것은 콘솔 창에서 다시 묻는 메시지입니다. –

답변

0

나는 의심의 여지가 여기 없다

/* Write a program that asks the user 
* to enter the starting point and end 
* point of the counting range and the 
* increment value and displays the total 
* of the numbers within that range 
*/ 

int start; 
int end; 
int increment; 
int sum = 0; 
int count= 0; 

Console.WriteLine(" Enter the start number "); 
start = Int32.Parse(Console.ReadLine()); 

Console.WriteLine(" Enter the end number "); 
end = Int32.Parse(Console.ReadLine()); 

Console.WriteLine(" Enter the increment number "); 
increment = Int32.Parse(Console.ReadLine()); 

for (count = start; //init value for count 
     count <= end ; //check every loop. if count still satify condition, then do thing inside tho loop 
     count += increment //change count every a loop done 
    ) 
{ 
    sum += count; 
    Console.WriteLine(" Number is: " + count); 

} 

Console.WriteLine(" Sum is: " + sum); 
Console.ReadKey(); 
+0

이것이 작동하는 것 같습니다! 고맙습니다 ! 도 for 문을 설명 할 수 있습니까? 1. 초기화 횟수 = 시작? 2. 가드 ... 카운트가 끝나지 않은 동안? 3. 진행하십시오 ... count = count + increment –

+0

위의 코드를 설명하려고하지만 자세한 내용은 https://www.dotnetperls.com/for를 참조하는 것이 좋습니다. – zquanghoangz

0

당신은 start < end 동안 루프를 원하고, 그 조건이 충족 될 때마다, 카운터는 increment 증가합니다.

이 루프의 구조가 될 것이다 :

for (int i = start; i < end; i += increment) 
{ 
    // Add to total and display current value 
} 
+0

시작 번호 이 가 증가 번호를 입력 끝 번호 입력 번호는 : 0 합이다 : 0 이 콘솔 창은 다시 나에게 묻지 무엇입니까 ?? –

+0

루프 앞에서'count'를 선언하지 마십시오. 루프는'for (int count = start; count

관련 문제