2016-10-21 5 views
2

사용자 입력에서 나이를 계산하는 방법을 사용하여 간단한 프로그램을 작성하려고합니다. 그러나 코드가 실행될 때 텍스트를 얻지 만 Age의 결과는 얻지 못합니다.왜 작동하지 않습니까? 예상 출력을 얻지 못했습니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace csharpExercises 
{ 
    class Program 
    {    
     public static int calcAge (int yourAge) { 
      int currentYear; 
      int year = 2016; 
      currentYear = year - yourAge; 
      return currentYear; 
     } 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Please enter the year you were born in: "); 
      int Age = int.Parse(Console.ReadLine()); 
      calcAge(Age); 
      Console.WriteLine("Your age is : ", Age); 
      Console.ReadKey(); 

     } 
    } 
} 
+0

이미 내가 –

답변

6

있어서 calcAge는 정수 값으로 정확하게 호출되고, 그것뿐만 아니라 정수를 반환한다.

  1. 당신은 그 호출 방법에서 돌아 정수 값을 표시/수신하지 않습니다

    두 가지 당신은주의해야합니다.

  2. 사용중인 표시 명령문의 형식이 올바르지 않거나 값 표시를위한 자리 표시자를 지정하지 않았습니다. 그렇지 않으면 출력을 연결하기 위해 +을 사용해야합니다.

호출이 같은 방법

Console.WriteLine("Your age is :{0}", calcAge(Age)); 

하거나 같은

Console.WriteLine("Your age is :" + calcAge(Age)); 

또는 이런

; 나는 때문에 따라 의견의 영업 이익 오프 주제로이 질문을 닫으 투표 해요

int currentAge=calcAge(Age); 
Console.WriteLine("Your age is :{0}", currentAge) 
+0

해결, + calcAge (Age)); 여전히 가치없는 결과물을 얻고 있습니다. – Overdrive

+0

Console.WriteLine ("당신의 나이는 :"와 같은 메소드를 호출하려면 코드를 변경 문제 – Overdrive

+0

Ok 고마워요. Console.WriteLine ("귀하의 나이 : {0}", calcAge (Age)); – Overdrive

관련 문제