簡體   English   中英

試圖在 c# 中制作它,用戶輸入他們的年齡並告訴他們他們出生的年份

[英]Trying to make it in c# where the user enters their age and it tells them what year they were born in

這是我到目前為止的代碼,我現在被困住了。

    static void Main(string[] args)
    {
        int age = 0;
        DateTime now = DateTime.Today;

        Console.WriteLine("What is your age?");
        age = Convert.ToInt32(Console.ReadLine());

    }

一個簡單的方法是創建一個字符串變量,它是一個 console.readline,將字符串轉換為 int(age),然后從當前年份中減去年齡,這個等式的結果將是他們出生的年份。 然而,這仍然不是完美的,好像你還沒有過生日,但它可能是一年的假期。

String stringAge = Console.ReadLine();
int age = Convert.ToInt32(stringAge);
//year is 2021
int result = age-2021;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM