Menu
Coddy logo textTech

결과 표시하기

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

challenge icon

챌린지

쉬움

우리 계산기 프로그램의 마지막 단계는 결과를 보기 좋게 포맷하여 표시하는 것입니다.

다음과 같이 계산기 프로그램을 완성하세요:

  1. 결과를 계산한 후, print("\n")을 사용하여 빈 줄을 추가하세요
  2. 새 줄에 텍스트 "Calculation Summary:"를 출력하세요
  3. 다음 줄에 전체 계산을 형식: "10.5 * 5.5 = 57.75"로 출력하세요 (실제 연산과 결과에 따라 조정하세요)
  4. 출력 문자열에 firstNumber, operation, secondNumber, result의 값을 삽입하기 위해 문자열 보간을 사용하세요

직접 해보기

void main() {
  print("Welcome to the Simple Calculator!");
  
  // 숫자 변수를 선언하고 초기화합니다
  double firstNumber = 10.5;
  double secondNumber = 5.5;
  
  // 숫자를 출력합니다
  print("First number: $firstNumber");
  print("Second number: $secondNumber");
  
  // 연산 변수를 선언하고 유효성 검사합니다
  String operation = "*";  // +, -, *, / 중 하나일 수 있습니다
  
  // 연산이 유효한지 확인합니다
  if (operation != '+' && operation != '-' && operation != '*' && operation != '/') {
    print("Invalid operation. Using addition (+) as default.");
    operation = '+';
  }
  
  print("Operation: $operation");
  
  // 계산을 수행합니다
  double? result;
  
  if (operation == '+') {
    result = firstNumber + secondNumber;
  } else if (operation == '-') {
    result = firstNumber - secondNumber;
  } else if (operation == '*') {
    result = firstNumber * secondNumber;
  } else if (operation == '/') {
    if (secondNumber == 0) {
      print("Error: Cannot divide by zero!");
      return;
    }
    result = firstNumber / secondNumber;
  }
  
  print("Result: $result");
  
  // 여기서 최종 결과를 포맷하고 표시합니다
}

기초의 모든 레슨