拒绝字符串变量中的字符串 - c#

我需要降低在控制台应用程序中编写字符串的能力,此刻,当输入文本而不是数字时,控制台崩溃。

我现在有一些类似的东西

class Program
{
    static void Main(string[] args)
    {
        string[] names = new string[2];
        string age;
        bool agetest = false;

        Console.WriteLine("Hello, I am the NameBot2000, What is your first name?");
        names[0] = Console.ReadLine();
        Console.WriteLine("Well done. What is your surname?");
        names[1] = Console.ReadLine();
        Console.WriteLine("What year were you born in?");
        age = Console.ReadLine();

        int.Parse(age);

        if (Enumerable.Range(0,2015).Contains(age));




        int year = 0;


        string wow = "";

        if (Enumerable.Range(0,31).Contains(year))
            wow = "young";

        else if (Enumerable.Range(31,51).Contains(year)) 
            wow = "old";

        else if (Enumerable.Range(51,500).Contains(year))
            wow = "ancient";

        Console.WriteLine("Well done. You said your name was {0} {1}, and you are {2} years old!", names[0], names[1], year);
        Console.WriteLine("You are so {0}!", wow);
        Console.ReadLine();


    }
}

我尝试合并一个布尔值,但不确定如何比较变量以检查其格式。

预先感谢堆积!

参考方案

代替Parse,使用TryParse

int age = 0;
if (Int32.TryParse(Console.Readline, out age)
    // Correct format.
else
    // error!

TryParse()的作用是接受用户输入,尝试将其解析为int,如果成功,则将输出int(和bool = true),否则将输出bool = false

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

Json到php,json_decode返回NULL - php

我正在用PHP进行JSON解析器的一些API,用于存储有关遗产的信息。我在解析时遇到问题,因为它返回的是NULL值而不是数组或对象。简单的JSON代码可以很好地解析,但是可以这样:{"success":true,"totalCount":1,"data":[{"id":99694…

将ajax的值存储到javascript变量中 - javascript

我有一个php文件,其中我从服务器获取数据。该php文件的输出是一个包含json格式数据的变量。PHP文件:<?php $dbHostName = "localhost"; $dbUserName = "venseld"; $dbUserPass = "wecuuu"; $dbName = &…