-3

ここで私のC#のコードは、if関数でboolステートメントを使用する方法を見つけるのに役立ちます。ここでは、C#で使用したすべてのクラスと関数のコードを示します。最後に、ブレークライトがどれだけあるかの結果を表示したい1 つの街灯柱に関するすべての情報を含む 2 行のライト

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 に答える