回 帖 发 新 帖 刷新版面

主题:请大家帮忙看看

using System; 
  using System.Collections.Generic; 
  using System.Text; 
  namespace inputCapitalLetter 
  { 
  class Program 
  { 
  static void Main() 
  { 
  [color=FF0000]bool ok = false; [/color]
  while (ok == false) 
  { 
  Console.Write("请输入5个大写字母:"); 
  string str = Console.ReadLine(); 
  if (str.Length != 5) 
  { 
  Console.WriteLine("你输入的字符个数不是5个,请重新输入。"); 
  } 
  else 
  { 
  ok = true; 
  for (int i = 0; i < 5; i++) 
  { 
  char c = str[i]; 
  if (c < A || c > Z) 
  { 
  Console.WriteLine("第{0}个字符“{1}”不是大写字母,请重新输入。", i + 1, c); 
  ok = false; 
  break; 
  } 
  } 
  } 
  } 
  } 
  } 
  } 


请问bool ok = false;在程序里有什么作用?
这样的好处是什么?
谢谢

回复列表 (共2个回复)

沙发

如果你不写 bool ok = false; 那 ok 就没有这个变量,值也就不知道,
会报错
  while (ok == false)    // ok 是哪里来的?
{
     .......
}

板凳

bool ok 是用来控制循环的.你前面也声明并初始化了.

我来回复

您尚未登录,请登录后再回复。点此登录或注册