A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 许万里 中级黑马   /  2013-2-25 18:01  /  1164 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这 代码明明没有问题   运行的时候就有问题什么情况

static void Main(string[] args)
        {
            Console.WriteLine("你是否要关机");
            string s = ReadAmswer();
            if (s == "y")
            {
                Console.WriteLine("关机");
            }
            else
            {
                Console.WriteLine("不关机");

            }
        }
        public static string ReadAmswer()
        {
            string result = "";
            do
            {
                result = Console.ReadLine();
                if (result != "y" || result != "n")
                {
                    Console.WriteLine("你输入的不正确请重新输入");
                }

            } while (result != "y" || result != "n");
            return result;

        }

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

5 个回复

倒序浏览
  1. static void Main(string[] args)
  2.         {
  3.             Console.WriteLine("你是否要关机");
  4.             string s = ReadAmswer();
  5.             if (s == "y")
  6.             {
  7.                 Console.WriteLine("关机");
  8.             }
  9.             else
  10.             {
  11.                 Console.WriteLine("不关机");

  12.             }
  13.         }
  14.         public static string ReadAmswer()
  15.         {
  16.             string result = "";
  17.             do
  18.             {
  19.                 result = Console.ReadLine();
  20.                 if (result != "y" && result != "n")
  21.                 {
  22.                     Console.WriteLine("你输入的不正确请重新输入");
  23.                 }

  24.             } while (result != "y"&& result != "n");
  25.             return result;
复制代码
这样就不会有问题了,你方法里应该使用&& 而不是||

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

回复 使用道具 举报
     if (result != "y" || result != "n")无论你输入什么,这都是true啊
改   if (result != "y"&&result != "n")

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

回复 使用道具 举报
你好   ,  if (result != "y" || result != "n")
                {
                    Console.WriteLine("你输入的不正确请重新输入");
                }

正确的是if (result != "y" && result != "n")
                {
                    Console.WriteLine("你输入的不正确请重新输入");
                }

条件错了   {:soso_e113:}

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

回复 使用道具 举报
用这种方法试试
static void Main(string[] args)
        {
            Console.WriteLine("你确定要关机吗?");
            string input = Console.ReadLine();
            input = ReadAmswer();
            if (input=="y")
            {
                Console.WriteLine("正在关机");
            }
            else
            {
                Console.WriteLine("取消关机");
            }
            Console.ReadKey();
        }
        public static string ReadAmswer()
        {
            string str = "";
            while (true)
            {
                if (str!="y" && str!="n")
                {
                    Console.WriteLine("只能输入y或n,请重新输入");
                    str = Console.ReadLine();
                }
                else
                {
                    break;
                }
            }
            return str;
                 
        }

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

回复 使用道具 举报
            do
            {
                result = Console.ReadLine();
                if (result != "y" && result != "n")
                {
                    Console.WriteLine("你输入的不正确请重新输入");
                }

            } while (result != "y" && result != "n");
这两个条件的问题
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马