控制台窗口刚关闭? C#

时间:2014-10-18 20:31:01

标签: c#

Soooo这个程序似乎合乎逻辑,而且我已经能够在c ++和java中创建具有相同想法的程序 每当我运行它时,一切都会突然关闭......也许我只是下载了错误的东西

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            /* 
            int[] numbahs= new int[5];
            numbahs[0] = 4;
            numbahs[1] = 3;
            Console.WriteLine(numbahs[1].ToString());

            string[] cow = new string[] { "pee", "poop" };
            foreach (string pee in cow)
            {

                Console.WriteLine(pee);
            }
            */

            int x;
            int y;
            Console.WriteLine("enter x:");
            x = Console.Read();
            Console.WriteLine("enter y:");
            y = Console.Read();
            int z = (x > y)? 8 : 2;
            Console.WriteLine("x:{0} y:{1} z:{2}",x ,y, z);
            if (z > x)
            {
                Console.WriteLine("yoyo");
                Console.Read();
            }
            else { Console.WriteLine("hihi"); }

            Console.Read();

        }
    }
}

2 个答案:

答案 0 :(得分:5)

使用Console.ReadLine

    int x;
    int y;

    Console.WriteLine("enter x:");
    x = int.Parse(Console.ReadLine());
    Console.WriteLine("enter y:");
    y = int.Parse(Console.ReadLine());

    int z = (x > y)? 8 : 2;

    Console.WriteLine("x:{0} y:{1} z:{2}",x ,y, z);
    if (z > x)
    {
        Console.WriteLine("yoyo");
    }
    else { Console.WriteLine("hihi"); }

    Console.ReadLine();

答案 1 :(得分:-1)

添加Console.ReadLine()代替Console.Read。

正确的解释如下:Why is the console window closing immediately without displaying my output?