新手学C#,问一个低级的问题,很容易,满意就给分。

jimkozyw 2010-09-29 03:36:36
protected void Button1_Click(object sender, EventArgs e)
{
int n;
string k;
k = TextBox1.Text;
if (k == n.ToString())
{
lbshow.Text = "恭喜猜中了!";
Label1.Text = "";
}
else
{
lbshow.Text = "很遗憾!没有猜中!";
Label1.Text = "这次的随机为" + n;
}



}
protected void bt2_Click(object sender, EventArgs e)
{

int n;
Random r = new Random();
n = r.Next(1, 10);
}


我希望的是单击开始游戏(bt2_Click) 生成随机数, 然后 在文本框输入 一个数字 ,接着 单击(Button1_Click()就会显示猜中还是没有猜中。。。

现在问题如下,因为是2个按钮,开始游戏生成的随机数不能再 单击(Button1_Click() 使用,,,如何解决????
...全文
216 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
dreammyboy 2010-09-29
  • 打赏
  • 举报
回复
fellowcheng的代码很好呀。。楼主怎么不用啊?fellowcheng兄,问一下,每次触发页面控件时是否都page_load
啊,比如简单的点击一个按钮之类的,还有能不能把无序说的详尽通俗一些。。
qq346127416 2010-09-29
  • 打赏
  • 举报
回复

要是b/s程序 用viewstate保存
protected void bt2_Click(object sender, EventArgs e) {

Random r = new Random();
viewstate["num"] = r.Next(1, 10);
}
protected void Button1_Click(object sender, EventArgs e)
{
string k;
k = TextBox1.Text;
if (k == viewstate["num"].ToString() )
{
lbshow.Text = "恭喜猜中了!";
Label1.Text = "";
}
else
{
lbshow.Text = "很遗憾!没有猜中!";
Label1.Text = "这次的随机为" + n;
}


如果c/s程序的话 把n放方法外面 static int n=0; 静态
hou_kui 2010-09-29
  • 打赏
  • 举报
回复
果然很低级~~~~
friedDuck 2010-09-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jimkozyw 的回复:]

不行啊 定义 全局 int的话 随机数不会变啊。。。一直都是0
[/Quote]

你原来有两个int n在button_click函数里面定义,两个都要去掉才行,移到函数外面定义。
fangxiaofelix 2010-09-29
  • 打赏
  • 举报
回复
貌似LZ是WEB页面啊,那就把产生的随机数存入session["Num"]=r.Next(1, 10).ToString()好了,这样就不会丢失
fangxiaofelix 2010-09-29
  • 打赏
  • 举报
回复
经测试,符合LZ要求!

public partial class Form2 : Form
{
private int current ;//当前产生的随机数
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

Random r = new Random();
current = r.Next(1, 10);
}

private void button2_Click(object sender, EventArgs e)
{
int n =Convert.ToInt32(textBox1.Text.ToString().Trim());
if (current == n)
{
MessageBox.Show("恭喜猜中了!");
}
else
{
MessageBox.Show("很遗憾!没有猜中!");
}
label1.Text=current.ToString();
}
}


界面


{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "随机数";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(12, 67);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "猜猜看";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(110, 14);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(108, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
}
}
lucky_yyx 2010-09-29
  • 打赏
  • 举报
回复
int n;
string k;
k = TextBox1.Text;
if (k == n.ToString())
{
lbshow.Text = "恭喜猜中了!";
lbshow.UPdate();
Label1.Text = "";
Label1.update();
}
else
{
lbshow.Text = "很遗憾!没有猜中!";
lbshow.UPdate();
Label1.Text = "这次的随机为" + n;
Label1.update();
}
tossgoon 2010-09-29
  • 打赏
  • 举报
回复
定义成全局的就可以。把N定义在外面:
private int n;
构造函数里最好初始化一下:
n=0;
一直为零可以是你没有重新编译?
raymond123456 2010-09-29
  • 打赏
  • 举报
回复
mark
fz069820 2010-09-29
  • 打赏
  • 举报
回复
你不应该这么设计 用户输入后,在去随即生成判断是否相等 就不需要保存任何的数据了
jimkozyw 2010-09-29
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 gx791264939 的回复:]
不懂你意思,直接把 int n; 定义到bt2_Click(object sender, EventArgs e)外面不就得了啊
[/Quote]

不行啊 生成的随机数都是0
gx791264939 2010-09-29
  • 打赏
  • 举报
回复
不懂你意思,直接把 int n; 定义到bt2_Click(object sender, EventArgs e)外面不就得了啊
fellowcheng 2010-09-29
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e) {
bt2_Click(null, null);
int.TryParse(hf1.Value, out n);
}

protected void Button1_Click(object sender, EventArgs e) {
string k;
k = TextBox1.Text;
if (k == n.ToString()) {
lbshow.Text = "恭喜猜中了!";
Label1.Text = "";
}
else {
lbshow.Text = "很遗憾!没有猜中!";
Label1.Text = "这次的随机为" + n;
}

}
int n;
protected void bt2_Click(object sender, EventArgs e) {

Random r = new Random();
n = r.Next(1, 10);
hf1.Value = n.ToString();
}
jimkozyw 2010-09-29
  • 打赏
  • 举报
回复
还有其他方法啊?
fellowcheng 2010-09-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jimkozyw 的回复:]

不行啊 定义 全局 int的话 随机数不会变啊。。。一直都是0
[/Quote]
web页面是无状态的,要保存的东西都要放在html表单里(比如<input type='hidden'>)
fellowcheng 2010-09-29
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApp._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">

</script>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Button ID="Button2" runat="server" onclick="bt2_Click" Text="Button" />
<asp:HiddenField ID="hf1" runat="server" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Label ID="lbshow" runat="server"></asp:Label>
</form>
</body>
</html>
fellowcheng 2010-09-29
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e) {
int.TryParse(hf1.Value, out n);
}

protected void Button1_Click(object sender, EventArgs e) {
string k;
k = TextBox1.Text;
if (k == n.ToString()) {
lbshow.Text = "恭喜猜中了!";
Label1.Text = "";
}
else {
lbshow.Text = "很遗憾!没有猜中!";
Label1.Text = "这次的随机为" + n;
}

}
int n;
protected void bt2_Click(object sender, EventArgs e) {

Random r = new Random();
n = r.Next(1, 10);
hf1.Value = n.ToString();
}
DevinHu 2010-09-29
  • 打赏
  • 举报
回复
全局变量是最简单的方法了,还有就是封装属性,项目上一班用
tu678 2010-09-29
  • 打赏
  • 举报
回复
1.在bt2_Click()中把随机数保存入 viewstate中
2.在Button1_click()点击时 读取viewstate中的值
3.判断viewstate中的值与用户输入的值是否相同
jimkozyw 2010-09-29
  • 打赏
  • 举报
回复
不行啊 定义 全局 int的话 随机数不会变啊。。。一直都是0
加载更多回复(4)

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧