C#中二进制变量的并集和统一 [英] union and unity of binary variable in C#

查看:66
本文介绍了C#中二进制变量的并集和统一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我为C#中的二进制变量的并集和统一编写了这段代码,但是当我运行它时,控制台只是打开和关闭.有什么问题吗?

Hi All,
I write this code for union and unity of binary variable in C# but when I run it, the console just open and close. what is its problem?

<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
public class CBoolOperate
{
    int[] a = null;
    int[] b = null;
    int[] c = null;
    CBoolOperate()//default constructor
    {
    }
    ~CBoolOperate()//default destructor
    {
    }
    public void get()
    {
        Console.WriteLine("please enter first 5 binary digit\n");
        for (int i = 0; i < 5; i++)
        {
            a[i] = Console.Read();
        }
        Console.WriteLine("please enter first 5 binary digit\n");
        for (int i = 0; i < 5; i++)
        {
            b[i] = Console.Read();
        }
    }
    public void union()//A member function
    {
        for (int i = 0; i < 5; i++)
        {
            if (a[i] == 1 || b[i] == 1) c[i] = 1;
            else c[i] = 0;
            Console.WriteLine(c[i]);
        }
        Console.WriteLine("");
    }
    public void and()//A member function
    {
        for (int i = 0; i < 5; i++)
        {
            if (a[i] == 1 && b[i] == 1) c[i] = 1;
            else c[i] = 0;
            Console.WriteLine(c[i]);
        }
        Console.WriteLine("");
    }
    public int main()
    {
        CBoolOperate r=new CBoolOperate();
        r.get();
        r.union();
        r.and();
        return 0;
    }
}




请帮我.

在此先感谢.




Please help me.

Thanks in Advance.

推荐答案

您好,亲爱的,
我认为您是编程的新手
我纠正了您的代码,并且效果很好
因为CBool​​Operate的构造函数是私有的,所以我将CBool​​Operate.main设置为static,相反它可能是

Hello dear,
I think you are a beginner in programing
I correct you code and it works well
Because constructor of CBoolOperate was private i make CBoolOperate.main as static , instead it could be

public CBoolOperate()//default constructor
{
}
and 
static void Main(string[] args)
{
    CBoolOperate A = new CBoolOperate();
    A.main();
    Console.ReadLine();
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            CBoolOperate.main();
            Console.ReadLine();
        }
    }
}
public class CBoolOperate
{
    int[] a = null;
    int[] b = null;
    int[] c = null;
    CBoolOperate()//default constructor
    {
    }
    ~CBoolOperate()//default destructor
    {
    }
    public void get()
    {
        this.a = new int[5];
        this.b = new int[5];
        this.c = new int[5];
        Console.WriteLine("please enter first 5 binary digit\n");
        for (int i = 0; i < 5; i++)
        {
            a[i] = Convert.ToInt32(Console.ReadLine());
        }
        Console.WriteLine("please enter first 5 binary digit\n");
        for (int i = 0; i < 5; i++)
        {
            b[i] = Convert.ToInt32(Console.ReadLine());
        }
    }
    public void union()//A member function
    {
        Console.WriteLine("result of union for numbers");
        for (int i = 0; i < 5; i++)
        {
            if (a[i] == 1 || b[i] == 1) c[i] = 1;
            else c[i] = 0;
            Console.WriteLine(c[i]);
        }
        Console.WriteLine("");
    }
    public void and()//A member function
    {
        Console.WriteLine("result of and for numbers");
        for (int i = 0; i < 5; i++)
        {
            if (a[i] == 1 && b[i] == 1) c[i] = 1;
            else c[i] = 0;
            Console.WriteLine(c[i]);
        }
        Console.WriteLine("");
    }
    public static int main()
    {
        CBoolOperate r=new CBoolOperate();
        r.get();
        r.union();
        r.and();
        return 0;
    }
}


这是因为静态方法Program.Main(它是程序的真实入口点)不执行任何操作.您可能以为其他方法main是入口点.不.它不是程序的入口;它不是程序的入口.它不会被调用.

默认入口点是具有兼容签名的静态方法Main.您可以在项目的属性中对其进行更改.

顺便说一句:永远不要编写不做任何事情的构造函数或析构函数.由于垃圾收集器(GC)的操作,几乎从未使用过析构函数.

—SA
This is because your static method Program.Main (which is a real entry point of your program) does nothing. Your probably thought that your other method main is an entry point. No. It is not an entry point of the program; it won''t be called.

The default entry point is a static method Main with compatible signature. You can change it in your project''s Properties.

By the way: Don''t ever write a constructor or destructor which don''t do anything. Destructors are almost never used because of Garbage Collector (GC) operation.

—SA


这篇关于C#中二进制变量的并集和统一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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