github_39574078 2021-11-25 00:03 采纳率: 66.7%
浏览 91
已结题

do while里面用true,没办法跳出循环,return不好使,求解答


private static int[] UnionLottoArray()
        {
            //获取数据
            do
            {
                Console.WriteLine('\n' + "请输入7注彩票数:(格式:xx,xx,xx,xx)");
                string lotteryNumber = Console.ReadLine();
                string[] segmentation = lotteryNumber.Split(',');
                int[] unionLotto = Array.ConvertAll(segmentation, int.Parse);
                //1.是否输入了7个数
                //2.数字是否都是大于1小于33
                //3.是否有重复值
                if (unionLotto.Length < 7 || unionLotto.Length > 7)
                {
                    Console.WriteLine("输入有误,请重新输入1");
                }
                else
                {
                    for (int i=0;i< unionLotto.Length; i++)
                    {
                        if (unionLotto[i] < 1 || unionLotto[i] > 33)
                        {
                            Console.WriteLine("输入有误,请重新输入2");
                        }
                    }
                }
                for (int i=0; i< unionLotto.Length -1; i++)
                {
                    for (int j=i+1; j< unionLotto.Length; j++)
                    {
                        if (unionLotto[i] == unionLotto[j])
                        {
                            Console.WriteLine("有重复值,请重新输入");
                        }
                        Console.WriteLine(j);
                    }
                  //尝试过在这里加return,但是错误的时候就会跳出 
                }
            } while (true);
        }

请问我这段要怎么写才能跳出循环,备注一下:我这方法里面要三个条件都满足了才能跳出,并且如果有错误的情况就继续执行循环,直到完全成功才跳出。
必须用do while循环,要先获取数据,然后才能判断。用true做条件是因为,获取的数据都循环里面,没办法直接用做条件。
如果要加一个判断做跳出,实在想不到是用什么来做条件,求帮帮忙~

  • 写回答

4条回答 默认 最新

  • haiXinYang 2021-11-25 13:02
    关注
    
            private static int[] UnionLottoArray()
            {
                //获取数据
                do
                {
                    Console.WriteLine('\n' + "请输入7注彩票数:(格式:xx,xx,xx,xx)");
                    string lotteryNumber = Console.ReadLine();
                    string[] segmentation = lotteryNumber.Split(',');
                    int[] unionLotto = Array.ConvertAll(segmentation, int.Parse);
                    //1.是否输入了7个数
                    //2.数字是否都是大于1小于33
                    //3.是否有重复值
                    if (unionLotto.Length != 7)
                    {
                        Console.WriteLine("输入有误,请重新输入1");
                        continue;
                    }
                    for (int i = 0; i < unionLotto.Length; i++)
                    {
                        if (unionLotto[i] < 1 || unionLotto[i] > 33)
                        {
                            Console.WriteLine("输入有误,请重新输入2");
                            unionLotto = null;
                            break;
                        }
                    }
                    if (unionLotto == null) continue;
                    for (int i = 0; i < unionLotto.Length - 1; i++)
                    {
                        for (int j = i + 1; j < unionLotto.Length; j++)
                        {
                            if (unionLotto[i] == unionLotto[j])
                            {
                                Console.WriteLine("有重复值,请重新输入");
                                unionLotto = null;
                                break;
                            }
                        }
                        if (unionLotto == null) break;
                    }
                    if (unionLotto == null) continue;
                    else
                    {
                        Console.WriteLine("这回肯定行");
                        return unionLotto;
                    }
                } while (true);
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 12月3日
  • 已采纳回答 11月25日
  • 创建了问题 11月25日