Codegym University 과정의 일부로 멘토와 함께하는 강의 스니펫. 전체 과정에 등록하십시오.


"안녕하세요, 아미고. 오늘은 if/else 문 에 대해 이야기하겠습니다 ."

"변화하는 외부 환경에 반응하지 않는다면 프로그램은 거의 쓸모가 없을 것입니다. 프로그램은 환경에 적응하고 어떤 경우에는 하나의 작업을 수행하고 다른 경우에는 다른 작업을 수행하는 방법을 알아야 합니다. Java에서는 다음을 사용하여 이 작업을 수행합니다. 'if/else 문' – 조건이 충족되면 다른 코드 블록을 수행할 수 있게 해주는 특수 구문입니다."

"' 조건 ', ' 명령 1 ' 및 ' 명령 2 ' 의 세 부분으로 구성됩니다 . 조건이 참이면 ' 명령 1 '이 실행되고 그렇지 않으면 '명령 2' 가 실행됩니다. 이 명령은 둘 다 실행되지 않습니다. 진술은 대체로 다음과 같습니다."

if/else 문의 코드
if (condition)
    command_1;
else
    command_2;

"얼마나 신나는 일인가! 그 말이 프로그래밍을 훨씬 더 흥미롭게 만들 것이라고 생각한다!"

"예. 다음은 몇 가지 예입니다."

암호 설명
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
a가 b보다 작으면 첫 번째 명령이 실행됩니다. 그렇지 않으면 두 번째 명령이 실행됩니다 . 명령이 둘 다 실행되지는 않습니다.
2
if (a < b)
{
    System.out.println("A is less than B");
    System.out.println("B is greater than A");
}
else
{
     System.out.println("B is less than A");
     System.out.println("A is greater than B");
}
하나의 명령을 코드 블록으로 바꿀 수 있습니다. 나머지는 동일합니다.
if (a < b)
{
    a = 0;
}
else
{
}
비어 있는 경우 else 블록을 생략할 수 있습니다 .
이 세 가지 예는 완전히 동일합니다.
하나의 명령만 실행해야 하는 경우 중괄호를 생략할 수 있습니다. 둘 이상의 명령이 있는 경우 대괄호를 유지해야 합니다.
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

"디에고는 당신에게 몇 가지 일을 맡기라고 했습니다."

2
과제
Java Syntax,  레벨 4레슨 4
잠금
Good or bad?
Student robot Peter is an overachiever. Previously, his server was configured to receive scores on a five-point scale, but now his teachers have switched to a 12-point scale. But Peter doesn't know this. He's still focused on getting fives. Let's write him a compare method that compares any number with five.
4
과제
Java Syntax,  레벨 4레슨 4
잠금
Closest to 10
Ten is extremely popular and attractive number. Everyone wants to be a ten. Or at least as close to it as possible. Two numbers are standing around wondering which of them is cooler. Answer: whichever is closer to ten. Let's write these numbers a displayClosestToTen method that will determine which of them is cooler.
4
과제
Java Syntax,  레벨 4레슨 4
잠금
Come on, lucky seven!
Dice games are popular on the planet Foggy Multidimensions. The rules are different than the Earth version: Multidimensionals perceive far more dimensions than primitive three-dimensional earthlings. Their die has 4294967295 faces. Players win only if they roll a number between 50 and 100. We'll write a method that checks whether the die roll is in this range.
4
과제
Java Syntax,  레벨 4레슨 4
잠금
Seasons on Terra
An Earth year consists of four seasons, each of which lasts 3 months. While our ship was parked on this cradle of humanity, the Interplanetary Tax Service asked us to write a program to determine the season based on a number corresponding to the month of the year. We don't know why they want it. They say that it's none of our business. But they promised not to remain in our debt.
4
과제
Java Syntax,  레벨 4레슨 4
잠금
Positive and negative numbers
Diego is tall, but Alfredo is short. Rishi is experienced, but you're a "noob" programmer. Comparisons are unavoidable in life. And the same thing is true in programs. So we'll continue practicing comparisons and displaying stuff on the screen. This time let's compare the entered number with zero and manipulate it based on the result of the comparison.
4
과제
Java Syntax,  레벨 4레슨 4
잠금
Day of the week
Planet Terra still has "offices"—an outdated type of workspace. With current technology, there is no need for them, but earthlings tend to be nostalgic and haven't been in a hurry to eradicate them. Terrestrial office workers develop "TGIF syndrome": they constantly want to know what day of the week it is. Let's write a program for them!

Codegym University 과정의 일부로 멘토와 함께하는 강의 스니펫. 전체 과정에 등록하십시오.