Java에서 for 루프를 1이 아닌 증분으로 늘리는 방법
다음과 같은 for 루프가있는 경우 :
for(j = 0; j<=90; j++){}
잘 작동합니다. 그러나 다음과 같은 for 루프가있을 때 :
for(j = 0; j<=90; j+3){}
작동하지 않습니다. 누군가 나에게 이것을 설명해 주시겠습니까?
j+3
의 값을 변경하지 않기 때문 입니다 j
. 당신과 그를 교체해야 j = j + 3
하거나 j += 3
그래서 값 j
3으로 증가 :
for (j = 0; j <= 90; j += 3) { }
아무도 실제로 태클하지 않았으므로 Could someone please explain this to me?
나는 다음과 같이 할 것이라고 믿습니다.
j++
속기, 실제 작업이 아닙니다 (정말 그렇습니다.
j++
j = j + 1;
매크로 나 인라인 교체를 수행하는 것이 아니라는 점을 제외 하면 작업과 실제로 동일합니다 . 의 작동 i+++++i
과 그 의미 에 대해 여기에서 많은 논의가 있습니다 ( i++ + ++i
OR 로 해석 될 수 있기 때문에(i++)++ + i
그 결과 : i++
대 ++i
. post-increment
및 pre-increment
연산자 라고합니다 . 왜 그렇게 명명되었는지 짐작할 수 있습니까? 중요한 부분은 과제에서 어떻게 사용되는지입니다. 예를 들어 다음과 같이 할 수 있습니다. j=i++;
또는 j=++i;
이제 예제 실험을 수행합니다.
// declare them all with the same value, for clarity and debug flow purposes ;)
int i = 0;
int j = 0;
int k = 0;
// yes we could have already set the value to 5 before, but I chose not to.
i = 5;
j = i++;
k = ++i;
print(i, j, k);
//pretend this command prints them out nicely
//to the console screen or something, it's an example
i, j, k의 값은 무엇입니까?
나는 당신에게 대답을주고 당신이 그것을 해결하도록 할 것입니다.)
i = 7, j = 5, k = 7;
이것이 사전 및 사후 증분 연산자의 힘이며 잘못 사용하면 위험합니다. 그러나 다음은 동일한 작업 순서를 작성하는 다른 방법입니다.
// declare them all with the same value, for clarity and debug flow purposes ;)
int i = 0;
int j = 0;
int k = 0;
// yes we could have already set the value to 5 before, but I chose not to.
i = 5;
j = i;
i = i + 1; //post-increment
i = i + 1; //pre-increment
k = i;
print(i, j, k);
//pretend this command prints them out nicely
//to the console screen or something, it's an example
좋아, 이제 ++
연산자가 어떻게 작동 하는지 보여 주 었으니, 왜 작동하지 않는지 살펴 보자. j+3
이전에 "속기"라고 불렀던 기억 나? 그게 전부입니다. 두 번째 예제를 보십시오. 컴파일러가 명령을 사용하기 전에 효과적으로 수행 하는 작업 이기 때문입니다 (그것보다 더 복잡하지만 첫 번째 설명은 아닙니다). 따라서 "확장 된 속기"에 i =
AND i + 1
가 있고 요청에 포함 된 모든 것이 표시됩니다.
이것은 수학으로 돌아갑니다. 함수는 어디에 f(x) = mx + b
또는 방정식으로 정의 y = mx + b
되므로 우리가 무엇이라고 부르는가 mx + b
... 확실히 함수 나 방정식이 아닙니다. 기껏해야 표현입니다. 그게 전부 j+3
입니다. 할당이없는 표현식은 우리에게 좋지 않지만 CPU 시간을 차지합니다 (컴파일러가이를 최적화하지 않는다고 가정).
이 내용이 여러분에게 명확하고 새로운 질문을 할 수있는 여지를 제공하기를 바랍니다. 건배!
귀하의 예에서는 j+=3
3 씩 증가합니다.
(여기에서 말할 것도 많지 않습니다. 구문과 관련된 경우 먼저 인터넷 검색을 제안하지만 여기에 새로 왔기 때문에 틀릴 수 있습니다.)
for(j = 0; j<=90; j = j+3)
{
}
j+3
j에 새 값을 할당하지 않고 add j=j+3
는 j에 새 값을 할당하고 루프는 3만큼 위로 이동합니다.
j++
is like saying j = j+1
, so in that case your assigning the new value to j just like the one above.
Change
for(j = 0; j<=90; j+3)
to
for(j = 0; j<=90; j=j+3)
It should be like this
for(int j = 0; j<=90; j += 3)
but watch out for
for(int j = 0; j<=90; j =+ 3)
or
for(int j = 0; j<=90; j = j + 3)
Simply try this
for(int i=0; i<5; i=i+2){//value increased by 2
//body
}
OR
for(int i=0; i<5; i+=2){//value increased by 2
//body
}
The "increment" portion of a loop statement has to change the value of the index variable to have any effect. The longhand form of "++j" is "j = j + 1". So, as other answers have said, the correct form of your increment is "j = j + 3", which doesn't have as terse a shorthand as incrementing by one. "j + 3", as you know by now, doesn't actually change j; it's an expression whose evaluation has no effect.
If you have a for loop like this:
for(j = 0; j<=90; j++){}
In this loop you are using shorthand provided by java language which means a postfix operator(use-then-change) which is equivalent to j=j+1 , so the changed value is initialized and used for next operation.
for(j = 0; j<=90; j+3){}
In this loop you are just increment your value by 3 but not initializing it back to j variable, so the value of j remains changed.
for(j = 0; j<=90; j++){}
j++ means j=j+1, j value already 0 now we are adding 1 so now the sum value of j+1 became 1, finally we are overriding the j value(0) with the sum value(1) so here we are overriding the j value by j+1.So each iteration j value will be incremented by 1.
for(j = 0; j<=90; j+3){}
Here j+3 means j value already 0 now we are adding 3 so now the sum value of j+3 became 3 but we are not overriding the existing j value. So that JVM asking to the programmer, you are calculating the new value but where you are assigning that value to variable(i.e j). That's why we are getting compile time error " invalid AssignmentOperator ".
If we wan't to increment j value by 3 then we can use any one of following way.
for (int j=0; j<=90; j+=3) --> here each iteration j value will be incremented by 3.
for (int j=0; j<=90; j=j+3) --> here each iteration j value will be incremented by 3.
You can also write code as
for(int i=0;i<n;i++)
{
//statements;
i=i+2;//cause you want to increment i by 3
}
'Programing' 카테고리의 다른 글
노드가 'N'인 경우 가능한 이진 및 이진 검색 트리는 몇 개입니까? (0) | 2020.11.07 |
---|---|
Scala-배열 인쇄 (0) | 2020.11.07 |
github에서 여러 저장소를 쉽게 언 워치하는 방법은 무엇입니까? (0) | 2020.11.07 |
AngularJS를 사용하여 새 창에서 링크 열기 (0) | 2020.11.07 |
부트 스트랩에서 열 고정 위치 만들기 (0) | 2020.11.07 |