1,Console.WriteLine("请输入数字"); string s=Console.ReadLine(); int number=Convert.ToInt32(s); while(s!="q") {Console.WriteLine("{0}",number*2); } 错误之处在于输入q时,报错。因为,不能字母转换为数字,应该在使用的时候再转。
Console.WriteLine("请输入数字"); string s=Console.ReadLine(); while(s!="q") { int number=Convert.ToInt32(s);
Console.WriteLine("{0}",number*2);还可以这么写,先设置一个死循环,
while(true)
{console.writeline("请输入一个数字");
string s==console.readline();
if (s=="q")
return;//执行到return的时候就退出整个函数(main),while这个死循环就不会再循环下去了。而控制台程序一旦main退出,程序就退出了
}
int number=Convert.ToInt32(s);
2, int max = 0; Console.WriteLine("请输入数字"); string s = Console.ReadLine(); if (s != "end") { int s1 = Convert.ToInt32(s); if (s1 > max) { max = s1; } } else { Console.WriteLine("{0} ",max); } {Console.WriteLine("{0}",number*2); 错误。
正确:
int max = 0; while (true) { Console.WriteLine("请输入一个数字,结束请输入end"); string s = Console.ReadLine(); if (s == "end") { Console.WriteLine("最大值为:{0}",max); Console.ReadKey(); return; } int number=Convert.ToInt32(s); if (number > max) { max = number; } }