在方法中使用do while语句返回字符串

本文关键字:while 语句 返回 字符串 do 方法 | 更新日期: 2023-09-27 18:12:15

我正在写一个购物系统的程序。在这里,我使用数组从用户的品牌名称获取输入。我正在使用返回字符串的方法来获取输入。下面是代码:

public class Brand{
 private string brandName;
    public string BrandName
    {
        get { return brandName; }
        set { brandName = value; }
    }
    public string getBrandName()
    {
        string[] brands = new string[5];
        brands[0] = "Honda";
        brands[1] = "Suzuki";
        brands[2] = "Ferrari";
        brands[3] = "BMW";
        brands[4] = "Toyota";
        Console.WriteLine("Please enter the brand name from the above given   brands..");
        string temp = Console.ReadLine();
        do
        {
            try
            {
                for (int i = 0; i < 6; i++)
                {
                    if (brands[i].Contains(temp))
                    {
                        this.BrandName = temp;
                        break;
                    }
                }
                return this.BrandName;
            }
            catch
            {
                Console.WriteLine("Your provide brand does not match with the database in our system. Please try another one.");
            }
        } while (BrandName!=temp);

    }
    }

问题是我是初学者,不知道这个while语句中应该有什么,它循环并要求用户一次又一次地输入,直到他输入正确的品牌名称。请帮帮我。

在方法中使用do while语句返回字符串

也许这将根据您的代码工作:

public string getBrandName()
{
    string[] brands = new string[5];
    brands[0] = "Honda";
    brands[1] = "Suzuki";
    brands[2] = "Ferrari";
    brands[3] = "BMW";
    brands[4] = "Toyota";
    Console.WriteLine("Please enter the brand name from the above given   brands..");
    string temp = Console.ReadLine();
    while(!brand.Contains(temp))
    {
        Console.WriteLine("Your provide brand does not match with the database in our system. Please try another one.");
        temp = Console.ReadLine();
    }
    return temp;
}

注意事项:

  1. 我们将询问用户的品牌名称。

  2. 我们将检查输入是否是来自brands列表的品牌(您使用contains来检查输入是否在每个品牌名称的字符数组中,观察差异)。

  3. 如果then name在列表中,我们将不进入循环,并且我们将返回品牌名称

  4. 如果该名称不在列表中,我们将再次要求用户插入a

如果你只有4个品牌,那么你可以在while循环中尝试或声明所有品牌

while (input== 'brand1'||'brand2')

或者如果你的列表太大你可以把它们放在数组列表

中这样的

List <String> listClone = new ArrayList<String>(); 
       for (String string : list) {
           if(string.matches("(?i)(bea).*")){
               listClone.add(string);
           }
       }
    System.out.println(listClone);
  1. 这里有一个outtorange异常:For (int I = 0;我& lt;6;我+ +)
  2. 当输入字符串是指定字符串的子字符串而不是相等时,函数包含返回true或false !!
  3. 你到达这一行只有当有例外:控制台。您提供的品牌与我们系统中的数据库不匹配。请再试一个");

检查下面的代码-它工作良好:

类项目{静态void Main(string[] args){Brand = new Brand();字符串brandName = brand.getBrandName();控制台。WriteLine("您输入了正确的品牌名称!!");Console.WriteLine(名牌);Console.ReadLine ();}

    public class Brand
    {
        private string brandName;
        public string BrandName
        {
            get { return brandName; }
            set { brandName = value; }
        }
        public string getBrandName()
        {
            bool isValid = false;
            string temp = "";
            string[] brands = new string[5];
            brands[0] = "Honda";
            brands[1] = "Suzuki";
            brands[2] = "Ferrari";
            brands[3] = "BMW";
            brands[4] = "Toyota";
            Console.WriteLine("Please enter the brand name from the above given   brands..");
            while (!isValid)
            {
                for (int i = 0; i < brands.Length; i++)
                {
                    Console.WriteLine(brands[i]);
                }
                temp = Console.ReadLine();
                for (int i = 0; i < brands.Length; i++)
                {
                    if (brands[i] == temp)
                    {
                        this.BrandName = temp;
                        isValid = true;
                        break;
                    }
                    else
                    {
                        isValid = false;
                    }
                    if (i == brands.Length - 1)
                    {
                        Console.WriteLine("Your provide brand does not match with the database in our system. Please try another one.");
                    }
                }
            }
            return temp;
        }
    }
}