循环 我怎样才能让吠叫 5 次,狗跑 10 次

本文关键字:狗跑 循环 | 更新日期: 2023-09-27 18:25:57

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   namespace Assignment07
   {
    class Dog
    {
        public void bark(string dogsName)
        {
            int barking =5;
            while(barking>5)

            Console.WriteLine(dogsName + " is barking");
        }
        public void run(string dogsName)
        {
            int running =10;
            while (running > 10)
            Console.WriteLine(dogsName + " is running");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Dog fido = new Dog();
            fido.bark("Fido");
            fido.run("Fido");
            Console.Write("Hit any key to close"); Console.ReadKey(true);
        }

    }
}

代码不起作用。我无法循环。这是行不通的。我想不通让他们去 10 或 5 次。还有什么能帮我的吗?我很乐意弄清楚这一点,因为我已经坚持了一个小时或更长时间

循环 我怎样才能让吠叫 5 次,狗跑 10 次

这是更正后的吠叫代码,您也应该能够修复运行

    public void bark(string dogsName)
    {
        int barking = 0;
        while(barking < 5){
            barking = barking + 1;
            Console.WriteLine(dogsName + " is barking");
        }
    }

试试这个类。

public class Dog
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int BarksOut { get; set; }
    public int RunOuts { get; set; }
    public Dog(string name, string id)
    {
        Name = name;
        Id = id;
    }
    public void Bark(int barksNumber)
    {
        for(var i =0; i < barksNumber; i++)
            Console.WriteLine("{0} >> {1} is barking", Id, Name);
        BarksOut += barksNumber;
    }
    public void Run(int runs)
    {
        for (var i = 0; i < runs; i++)
            Console.WriteLine("{0} >> {1} is running", Id, Name);
        RunOuts += runs;
    }
    public void Lie(long lies)
    {
        if (lies == 0)
            return;
        for (var i = 0; i < lies; i++)
            Console.WriteLine("{0} >> {1} if lying", Id, Name);
    }
}

作为使用示例:

        var dog = new Dog("Fido", "K9");
        dog.Bark(5);
        dog.Run(10);
        dog.Lie(0)

输出为:

    K9 : Fido is barking
    K9 : Fido is barking
    K9 : Fido is barking
    K9 : Fido is barking
    K9 : Fido is barking
    K9 : Fido is running
    K9 : Fido is running
    K9 : Fido is running
    K9 : Fido is running
    K9 : Fido is running
    K9 : Fido is running
    K9 : Fido is running
    K9 : Fido is running
    K9 : Fido is running
    K9 : Fido is running

玩得愉快!

int barking =5;
while(barking>5)

由于吠叫永远不会高于五,因此 while 循环永远不会运行。

 int running =10;
 while (running > 10)

同样的问题在这里。

试试这个:

    public void bark(string dogsName)
    {
        int barking = 0;
        while(barking < 5)
        {
            Console.WriteLine(dogsName + " is barking");
            barking++;
        }
    }

我会让你自己修复运行方法。

更新,以显示如何在方法之外进行循环

更改 bark 方法,使其不会循环:

public void bark(string dogsName)
{
    Console.WriteLine(dogsName + " is barking");
}

相反,在循环中调用 bark(( 方法:

static void Main(string[] args)
{
    Dog fido = new Dog();
    int barking = 0;
    while (barking < 5)
    {
         fido.bark("Fido");
         barking++;
    }
    Console.Write("Hit any key to close"); Console.ReadKey(true);
}