Menu
Coddy logo textTech

대입 단축

Coddy Dart 여정의 기초 섹션에 포함된 레슨. 94개 중 20번째.

Dart에서 assignment shortcuts는 산술 연산자와 대입을 결합하여 variables를 간결하게 수정합니다.

1. 덧셈 대입 (+=)

void main() {
  int score = 10;
  score += 5;  // 다음과 같음: score = score + 5;
  print('Score after adding 5: $score');
}

2. 뺄셈 대입(-=)

void main() {
  int lives = 3;
  lives -= 1;  // 다음과 같음: lives = lives - 1;
  print('Lives after losing 1: $lives');
}

3. 곱셈 대입 (*=)

void main() {
  int points = 5;
  points *= 2;  // 다음과 동일: points = points * 2;
  print('Points after doubling: $points');
}

4. 나눗셈 대입 (/=)

void main() {
  double price = 100.0;
  price /= 2;  // 다음과 같음: price = price / 2;
  print('Price after 50% discount: $price');
}
challenge icon

챌린지

쉬움

대입 단축 연산자를 사용하여 플레이어의 게임 통계를 추적하는 프로그램을 만드세요:

  1. 초기값이 100인 정수 변수 score를 Declare하세요
  2. 초기값이 50인 정수 변수 bonus를 Declare하세요
  3. 초기값이 2인 정수 변수 multiplier를 Declare하세요
  4. 초기값이 30인 정수 변수 penalty를 Declare하세요
  5. 다음 메시지와 함께 초기 점수를 Print하세요: Initial score: 100
  6. += operator를 사용하여 bonus를 score에 더하세요
  7. bonus를 더한 후의 score를 Print하세요: Score after bonus: 150
  8. *= operator를 사용하여 score에 multiplier를 곱하세요
  9. multiplier를 적용한 후의 score를 Print하세요: Score after multiplier: 300
  10. -= operator를 사용하여 score에서 penalty를 빼세요
  11. 최종 score를 Print하세요: Final score: 270

출력은 위에 표시된 정확한 형식과 일치해야 합니다.

직접 해보기

void main() {
  // 여기에 변수를 선언하세요
  // 아래의 플레이스홀더 값을 과제의 값으로 바꾸세요
  int score = 0;
  int bonus = 0;
  int multiplier = 0;
  int penalty = 0;
  
  // Print initial score
  print("Initial score: $score");
  
  // += 연산자를 사용하여 보너스를 더하세요
  score = bonus;
  
  // Print score after bonus
  print("Score after bonus: $score");
  
  // *= 연산자를 사용하여 배수를 적용하세요
  score = multiplier;
  
  // Print score after multiplier
  print("Score after multiplier: $score");
  
  // -= 연산자를 사용하여 페널티를 빼세요
  score = penalty;
  
  // 최종 점수를 출력하세요
  print("Final score: $score");
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

기초의 모든 레슨

직접 연습해 보세요: 온라인 Dart 컴파일러