小编Eri*_*ric的帖子

在非常简单的代码中,类,结构或接口成员声明中的令牌"while"无效

我不确定问题是什么,但当我尝试在我的代码中使用while语句时,我一直收到此错误.

类,结构或接口成员声明中的标记'while'无效

我想使用while循环在声明为真时持续更新某些内容.

我的其余代码相当长,但每当我输入语法时:

while(a<b)
{
//do whatever i want it to do here
}
Run Code Online (Sandbox Code Playgroud)

它立刻给了我编译器错误.不太确定问题是什么.我在Form1.cs文件下的C#windows应用程序中使用所有其他事件处理程序(对于按钮等)执行此操作.谢谢!


我不知道循环必须放在一个方法中(相当新的c#),但我尝试了它并没有返回任何错误.感谢大家的帮助!

以前,我只是在程序的主类中有循环.

c# loops while-loop

7
推荐指数
1
解决办法
3万
查看次数

RandomNumber方法每次调用都返回相同的数字

我每次从for循环中调用RandomNumber方法时,我都会尝试生成一个不同的随机数.现在,它每次都返回相同的数字.

这是我的RandomNumber方法:

    private int RandomNumber(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }
Run Code Online (Sandbox Code Playgroud)

这是我使用它的上下文:(它有点乱,因为我一直在试图让它工作......)

        for (int i = 0; i < charsRaw.Length; i++)
        {
            int max = charsRaw.Length - 1;
            int rand = 0;
            rand = RandomNumber(0, max);

            charsNew[i] = charsRaw[rand];
            text2 += charsNew[i];

         }
Run Code Online (Sandbox Code Playgroud)

每次使用for循环调用时,我似乎都无法返回不同的值.

虽然,当我在text2 + = charsNew [i]之后粘贴MessageBox.Show(rand.ToString())时,它每次都给我一个不同的值,并按照我的预期方式工作.奇怪.

谢谢!埃里克

c# random

6
推荐指数
1
解决办法
4180
查看次数

非静态字段,方法或属性需要对象引用吗?

我知道这可能是一个非常新的问题,所以我道歉.

我试图从另一种形式MaxScore访问Form1上的标签的Text属性.

当我单击MaxScore上的Ok按钮时,我想通过使用max.ToString()将Form1的myGameCountLbl.Text设置为Form1的变量max.

这是我在MaxScore的OK按钮事件中的代码:

private void okBtn_Click(object sender, EventArgs e)
{
    Form1.myGameCountLbl.Text = Form1.max.ToString();
    Form1.compGameCountLbl.Text = Form1.max.ToString();
}
Run Code Online (Sandbox Code Playgroud)

但是当我去编译它时,我得到错误:

非静态字段,方法或属性"Towergame_2.Form1.myGameCountLbl"需要对象引用

我得到了Towergame_2.Form1.max和Towergame_2.Form1.compGameCountLbl的相同错误.

不太确定如何解决这个问题.Max是一个公共变量,两个标签也是公共变量.

谢谢!

这是我的构造函数中的代码(谢谢lassevk的代码!):

public Form1()
{
    //initialize vars
    myHp = 100;
    compHp = 100;
    youWon = 0;
    compWon = 0;
    money = 100;
    canCompAttack = true;
    gameOver = false;

    //show HowToPlay Dialogue
    HowToPlay howToPlay = new HowToPlay();
    howToPlay.ShowDialog();

    using (MaxScore maxScore = new MaxScore())
    {
        maxScore.MainForm = this;
        maxScore.ShowDialog();
    }

    InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)

c# properties reference object

5
推荐指数
1
解决办法
2万
查看次数

新的C#Programmer - 一起添加两个numericUpDown值?

我是C#编程的新手.

我正在制作一个有趣的程序,它将两个数字加在一起,而不是在消息框中显示总和.我的表单上有两个numericUpDowns和一个按钮.按下按钮时,我希望它显示一个带答案的消息框.

问题是,我不确定如何将numericUpDowns中的twp值一起添加.

到目前为止,我在我的按钮事件处理程序中有这个:

private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(this.numericUpDown1.Value + this.numericUpDown2.Value);
    }
Run Code Online (Sandbox Code Playgroud)

但显然,它不起作用.它给了我2个编译器错误:1.'System.Windows.Forms.MessageBox.Show(string)的最佳重载方法匹配有一些无效的参数2.参数'1':不能将十进制转换为'string'

谢谢!

c# numericupdown

2
推荐指数
1
解决办法
7426
查看次数

使用CSS样式无法摆脱​​HTML页面底部的空间

我最近发布了一个关于使用CSS来居中页面的问题.我想出了如何做到这一点,它看起来很棒,但现在,我的页面底部有一堆不必要的空间.我无法弄清楚如何摆脱它.

我将它上传到一个快速免费的虚拟主机,所以你们可以看看.这就是我正在尝试使用的内容:http://eric.200u.com/index.html

这是我关于页面居中的CSS:

html, body 
{
    padding: 0;
    margin: 0;
}

#container 
{
    width: 700px;
    margin: 0 auto;
    text-align: left;
}
Run Code Online (Sandbox Code Playgroud)

html css

1
推荐指数
1
解决办法
3438
查看次数

无法使CSS元素居中

我无法通过CSS让我的网站以我的生活为中心.我已经尝试了网络上建议的所有常用方法,包括:

body {
    text-align: center;
}
#container {
    width: 770px;
    margin: 0 auto;
    text-align: left;
}
Run Code Online (Sandbox Code Playgroud)

然后使用

<div id="container>
<!-- Centered Content Goes here-->
</div>
Run Code Online (Sandbox Code Playgroud)

但它不会去中心.它停留在页面的左侧.

我想要居中的元素的CSS示例如下:

#topHeader
{
    background:url(images/top_header.jpg);
    position:absolute;
    width: 695px;
    height: 242px;
    top: 0px;
    left: 0px;
}
Run Code Online (Sandbox Code Playgroud)

所以,我的HTML看起来像这样:

<div id="container>
<div id="topHeader></div>
<!-- All other elements go here as well-->
</div>
Run Code Online (Sandbox Code Playgroud)

但正如我之前提到的,元素仍然存在.谢谢!埃里克

css elements

0
推荐指数
1
解决办法
189
查看次数

标签 统计

c# ×4

css ×2

elements ×1

html ×1

loops ×1

numericupdown ×1

object ×1

properties ×1

random ×1

reference ×1

while-loop ×1