C#2 if语句问题 [英] C# 2 if statements problem

查看:75
本文介绍了C#2 if语句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写代码



i'm trying to write a code

if (question == 1)
        {
            if (textAnswer.Text.Equals("adult"))
            {
                points = points + 500;
                scoreLabel.Text = "POINTS : " + points;
                okuB();

            }
            else if (textAnswer.Text != "adult")
            {
                points = points - 250;
                scoreLabel.Text = "POINTS : " + points;

            }

        }





如果用户写成人,他将获得500积分,如果他写了别的东西,他将失去250分。当我写第一次我失去250分后,我写了正确的单词,但我得到250分不是500.我怎么能解决这个问题?我从1000点开始



我尝试过:



well我试过,如果,否则,积分+ = 500,积分 - = 250但仍然无法正常工作



if user writes adult he will earn 500 points , if he writes something else he'll lose 250 points.When i write incorrent first i lose 250 points after that i write correct word but i get 250 points not 500.How can i fix this ? I started with 1000 points

What I have tried:

well i tried if , else if , else , points += 500 , points -= 250 but still it doesnt work

推荐答案

首先,你不需要一个否则如果声明在这里;一个 else 语句就足够了:

First of all, you do not need an else if statement here; an else statement is sufficient:
if (textAnswer.Text.Equals("adult"))
{
   // ...
}
else
{
   // ...
}



其次,数学。在您的实施中,您直接将奖金或malus应用于积分,但您不会跟踪这些变化。所以,当你第一次输入成人以外的东西时,积分变量变为1000 - 250 = 750.

然后你输入成人并且积分变量变为750 + 500 = 1250.

您最好有第二个奖金变量,用于计算最终积分:


Second, the maths. In your implementation, you apply directly the bonus or malus to the points, but you do not keep track of the changes. So, when you first enter something else than "adult", points variable becomes 1000 - 250 = 750.
Then you enter "adult" and points variable becomes 750 + 500 = 1250.
You would better have a second bonus variable, that you would use to compute the final points:

int bonus, total;
if (textAnswer.Text.Equals("adult"))
{
   bonus = 500;
   okuB();
}
else
{
   bonus = -250;
}
total = points + bonus;
scoreLabel.Text = string.Format("POINTS : {0}", total);



在您的实施中,您永远不应该更改直接变量,而是总是重新计算并显示总值。

如果有多个用户输入可以改变点数,那么你将需要一系列奖金,每个可能的用户一个输入。总数将是每个奖金/ malus的原始积分总和。



希望这会有所帮助。好的工作:)


In your implementation, you should never change the points variable directly, and instead always recompute and display the total value.
If there are several user inputs that can change the points, then you will need an array of bonuses, one for each possible user input. And the total would be the sum of the original points with every bonus/malus.

Hope this helps. Good work :)


首先要注意的是第二个文本是无关紧要的:如果第一个文本失败,那么第二个文本将永远成功:字符串要么匹配要么不匹配,它没有第三种方式。

所以我这样做的方式是:

The first thing to notice is that the second text is irrelevant: if the first fails, then the second will always succeed: a string either matches or it doesn't, there is no "third way" for it to be.
So the way I would do that would be:
if (question == 1)
    {
    if (textAnswer.Text.ToLower() == "adult")
        {
        points += 500;
        okuB();
        }
    else
        {
        points -= 250;
        }
    scoreLabel.Text = "POINTS : " + points;
    }



但是...快速测试(以及您的原始代码)显示没有您描述的问题。

如果点数从1000开始,那么最后,它是1500或750,没有其他价值。

所以我怀疑你需要查看你的 okuB 方法,看看它在做什么!

我从调试器开始:在线上放一个断点


But...a quick test of that (and your original code) shows no such problem as you describe.
If points starts at 1000, then at the end, it's either 1500 or 750, no other value.
So I suspect you need to look at your okuB method and see what that is doing!
I'd start with the debugger: put a breakpoint on the line

if (question == 1)

并逐步执行代码,通过Autos和Locals窗格查看确切的内容,以便在您继续操作时查看各种变量中的内容。

and step through the code, watching exactly what is going on via the "Autos" and "Locals" panes to see what is in the various variables as you proceed.


这篇关于C#2 if语句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆