日期:2014-05-17  浏览次数:21041 次

C#.NET编写的猜数字小游戏,有个问题
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class game : System.Web.UI.Page
{
  public int Rannumber() //由计算机产生一个随机数(0~10)的类
  {
  Random Ran;
  Ran = new Random();
  int i = Convert.ToInt32(Ran.NextDouble() * 10);
  return i;
  }
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  protected void btnguess_Click(object sender, EventArgs e)
  {
  Session["guess"] = guessnum.Text;
  int n = (int)Session["guess"];
  if (n == Rannumber())
  {
  Response.Write("<script>alert('恭喜你!你猜对了!');</script>");
  }
  else if (n > Rannumber())
  {
  Response.Write("<script>alert('请输入更小的数。');</script>");
  }
  else
  {
  Response.Write("<script>alert('请输入更大的数。');</script>");
  }
  }
}
编译时说int n = (int)Session["guess"];指定转换无效该怎么改?

------解决方案--------------------
Session["guess"] = guessnum.Text;
int n = (int)Session["guess"];

改为:
int n=(int)guessnum.Text;
------解决方案--------------------
服务器控件 多好用啊 各种拖……