-3

这里我在 c# 中的代码帮助我找到一种在 if 函数中使用 bool 语句的方法,这里是我在 c# 中使用过的所有类和函数的代码,最后我想显示有多少breaklights的结果在两行灯中,包含有关一根灯柱的所有信息

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

namespace Project1     
{
    public class LightPost       
    {
        private int noOfLightPost;
        private bool onOrOff;
        private string nameOfStreet;
        public LightPost(int noOfLightPost,bool onOrOff,string nameOfStreet)
        {
            this.nameOfStreet = nameOfStreet;
            this.onOrOff = onOrOff;
            this.nameOfStreet = nameOfStreet;
        } 
    }

    public class Line
    {
        private const int MaxLightpost=100;
        private int currentno;
        private int noOfTheLightpost;
        public LightPost[] lightposts;
        public Line(int maxLightpost, int currentno)
        {
            this.currentno = 0;
            this.noOfTheLightpost = noOfTheLightpost;

        }
        public void AddLightPost(LightPost l)
        {
            this.lightposts[currentno]=l;
            this.currentno++;    
        }


        static void Main(string[] args)
        {
            Line a1, a2;
            a1 = new Line(100, 67);
            a2 = new Line(100, 66);
            a1.AddLightPost(new LightPost(45, true, "kuf"));
            a1.AddLightPost(new LightPost(55, false, "kuf1")); a1.AddLightPost(new LightPost(7, true, "kuf42")); a1.AddLightPost(new LightPost(454, false, "kuf22"));
            a1.AddLightPost(new LightPost(48, false, "kuf3")); a1.AddLightPost(new LightPost(544, false, "kuf14")); a1.AddLightPost(new LightPost(564, true, "kuf34"));
            a1.AddLightPost(new LightPost(43, false, "kuf2")); a1.AddLightPost(new LightPost(555, true, "kuf46")); a1.AddLightPost(new LightPost(4445, true, "kuf5"));
            a2.AddLightPost(new LightPost(556,true,"ksfsd"));
            a2.AddLightPost(new LightPost(556,true,"kassd"));
            a2.AddLightPost(new LightPost(556,true,"kssds"));
            int i,brokelight;
            for(i=0;i<13;i++)
            {             
                if(onOrOff)
                {
                    brokelight++;
                }
            }
        }
    }
}
4

2 回答 2

1

虽然不清楚,但您可能正在寻找这个:

for(i=0;i<13;i++)
{
    if(ai.lightposts[i].onOrOff) //Use the index to access the Lightpost objects in your array.
    {
        brokelight++;
    }
}

onOrOff 也必须公开。

于 2013-08-15T10:49:46.477 回答
0

更改访问说明符

public int noOfLightPost{get;set;}
public bool onOrOff{get;set;}
public string nameOfStreet{get;set;}

然后你可以访问变量

于 2013-08-15T10:52:39.807 回答