尝试完成代码以将年龄与18进行比较并给出解决方案 [英] Trying to finish code to compare age to 18 and give solution

查看:72
本文介绍了尝试完成代码以将年龄与18进行比较并给出解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试制作一个简单的程序,让用户在控制台窗口中为我提供他的出生年份。然后我将从当前年份中减去它并将其与18进行比较。我为date.time.year做了一个int - userdateofbirth并且我试图将它与18进行比较以给出if then语句。如果超过18岁他们将能够继续,如果没有,那么他们会被拒绝。到目前为止我的代码是



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 ConsoleApplication2
{
class Program
{
静态 void Main( string [] args)
{
string 谢谢;
Console.WriteLine( 感谢您选择与我们一起购物);
Console.ReadLine();

string 诞生;
int Birthint;
Console.WriteLine( 要完成购买,请输入您的出生日期。 );
Birth = Console.ReadLine();
Birthint = Convert.ToInt32(出生);
Console.ReadLine();

Console.WriteLine( 今年是 + DateTime.Now。年);
Console.WriteLine( 你是 +(DateTime.Now.Year - Birthint) + 岁。);
Console.ReadLine();

int Oldint =(DateTime.Now.Year - Birthint);
Oldint = Convert.ToInt32(Oldint);






如果(Oldint > = 18


// 你已超过18

如果(Oldint < span class =code-keyword>< = 18

// 你未满18岁



Console.ReadLine();













}
}
}







任何建议从哪里开始?

解决方案

要计算年龄,请使用类似的东西:

 DateTime date1 =  new  DateTime( 2001  1  15 ); 
DateTime date2 = DateTime.Today;

int age = date2.Year - date1.Year;

Console.WriteLine( 你是{0} 18岁 ,年龄< 18? over);







有关时差计算的更多信息: TimeSpan Structure [ ^


I'm currently trying to make a simple program that will ask a user in a console window to give me his year of birth. I will then subtract it from the current year and compare it to 18. I made an int for date.time.year - userdateofbirth and am trying to compare that to 18 to give an if then statement . If over 18 they will be able to continue, if not then they get rejected. My code so far is

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string Thanks;
            Console.WriteLine("Thank you for choosing to shop with us");
            Console.ReadLine();

            string Birth;
            int Birthint;
            Console.WriteLine("To complete your purchase, please enter your date of birth.");
            Birth = Console.ReadLine();
            Birthint = Convert.ToInt32(Birth);
            Console.ReadLine();

            Console.WriteLine("This year is " + DateTime.Now.Year);
            Console.WriteLine("You are " + (DateTime.Now.Year - Birthint) + " years old.");
            Console.ReadLine();

            int Oldint = (DateTime.Now.Year - Birthint);
            Oldint = Convert.ToInt32(Oldint);






            if (Oldint >= 18)


                //you are over 18

            if (Oldint <= 18)

                //you are under 18



                Console.ReadLine();













        }
    }
}




any suggestions on where to go from here?

解决方案

To calculate age, use something similar to:

DateTime date1 = new DateTime(2001,1,15);
DateTime date2 = DateTime.Today;

int age = date2.Year - date1.Year;

Console.WriteLine("You are {0} 18 years old", age<18 ? "under" : "over");




More about time difference calculating:
TimeSpan Structure[^]


这篇关于尝试完成代码以将年龄与18进行比较并给出解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆