C# - 提供用户输入无效

时间:2017-06-18 19:30:15

标签: c#

所以我有这个代码。它需要用户输入,但它不会按我的意愿显示:

    double v = 0.1;
    string age;
    string name;
    string status;

    Console.WriteLine(v);

    Console.WriteLine("Hei buddy! What is your name?");
    name = Console.ReadLine();
    Console.WriteLine("Nice to meet you {0}. My name is PAI!", name);
    Console.WriteLine("How old are you {0}?", name);

    age = Console.ReadLine();
    Console.WriteLine("Wow. So you are {1}-years-old. That's a huge number, as I am only x day old.", age);

问题出在哪里?

2 个答案:

答案 0 :(得分:2)

这是我从理解你的问题中得出的解决方法。

        int v = 0;
        string age;
        string name;
        string status;

        Console.WriteLine(v);

        Console.WriteLine("Hei buddy! What is your name?");
        name = Console.ReadLine();
        Console.WriteLine("Nice to meet you {0}. My name is PAI!", name);
        Console.WriteLine("How old are you {0}?", name);

        age = Console.ReadLine();
        Console.WriteLine("Wow. So you are {0}-years-old. That's a huge number, as I am only x day old.", age);
        Console.ReadKey();

我不知道你为什么在float中分配了int值,所以这就是你的第一个问题所在。其次,您将值存储在status字符串中并尝试打印未使用的变量name。第三个错误发生在占位符{1}中,您需要输入年龄并使用占位符{1}而不是{0}进行打印。

答案 1 :(得分:0)

这是我修复的方式。我是输入/输出的新手,所以我很困惑。

    public static void Main(string[] args)
{
    double v = 0.1;
    string name;
    /*string status;*/

    Console.WriteLine(v);

    Console.WriteLine("Hei buddy! What is your name?");
    name = Console.ReadLine();
    Console.WriteLine("Nice to meet you {0}. My name is PAI!", name);
    Console.WriteLine("How old are you {0}?", name);

    int age = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Wow. So you are {0}-years-old. That's a huge number, as I am only x day old.", age);

我现在唯一需要知道的是,如何阻止控制台自动关闭。