Console.ReadKey();<div class="quote"><blockquote>循环中的i=0是为了防止比如用户输入一个数是8那么下次循环的时候 i 已经=8了输入8或者比8小的数字就不执行while循环了有什么更好的方法可以解决此类问题<div class="quote"><blockquote>除了i=0还有别的什么方法啊
复制代码
作者: 赵贺景 时间: 2014-5-12 23:28
什么意思 ? i提问 。这里的i是为了循环需要,你用调试走一边就知道 do里面语句要执行下去 就要保证i的值以初始值调用 即在最后 给i=0初值 使循环继续下去,直到输入q结束作者: 王运波 时间: 2014-5-13 10:33
利用try-catch 和while语句的嵌套判断用户输入是否正确,再自定义TT方法判断输入是否是数字,下面是我做的源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace tes1
{
class Program
{
//1、 不断要求用户输入一个数字,然后打印这个数字的二倍,当用户输入q的时候程序退出。
static void Main(string[] args)
{
do //第一层循环判断用户是否退出
{
Console.WriteLine("请输入一个数字:");
string _number = Console.ReadLine();//定义整形变量_number接受用户输入的值
TT t = new TT();
bool b = true; //定义bool类型b 判断用户输入类型
while (b)
{
if (t.b(_number))
{
int number = 2 * Convert.ToInt32(_number);//按题目要求,先乘二倍,BUG :用户输入范围在-2^16到2^16-1之间整数超出提示错误
Console.WriteLine("{0}的二倍等于:\n{1}",_number,number);
Console.WriteLine("退出请输入q 点回车键继续重新计算");
b = false;
}
else
{
Console.WriteLine("输入数字含有非数字请重新输入");
_number = Console.ReadLine();
}
}
}
while (Console.ReadLine() != "q");
}
private class TT // 新建类判断用户输入是否为数字
{
public bool b(string s)
{
string pattern = "^[0-9]*$";
Regex rx = new Regex(pattern);
return rx.IsMatch(s);
}