除已具有设定值的整数变量之外

问题描述 投票:0回答:2

这是我学习编码的第二天。我选择C ++作为我的第一语言,并决定创建一个cpp项目。该项目有4个问题,每个问题有2个答案(是和否)。最后,它必须指出结果,具体取决于正确答案的数量。我所做的一切都在工作,除了1件事。因此,例如,如果您至少错误回答了三个问题,您将收到提示<

正如我之前提到的,除了一件事情,我所做的一切都正确。为了跟踪多少个问题正确/不正确,设置变量:

int amount_of_correct_answers;
amount_of_correct_answers = 0;

然后我这样做,以便如果答案正确,它将在此变量上加1

if(answer == true_answer)
{
 amount_of_correct_answers + 1;
}
else
{
 amount_of_correct_answers + 0;
 }

因此,在测试结束时,您会看到结果(如果您是愚蠢或聪明的人)。我的问题是:如何添加/减去变量?如果答案正确,如何将1加到设为0的变量中?因为我上面写的代码没有用。我认为我走在正确的轨道上,我的问题是语法,因为我不知道如何向具有设定值的变量加减。

P.S。请记住,正如我之前提到的,我对编码非常陌生,因此请用简单的文字或示例进行解释。谢谢

c++ variables addition
2个回答
0
投票

所有这些都做:

amount_of_correct_answers + 1;

amount_of_correct_answers的值加1并丢弃结果。您需要将值分配回变量:

amount_of_correct_answers = amount_of_correct_answers + 1;

1
投票

如果您有一个名为amount_of_correct_answers的变量,则可以通过3种方式递增/递减它:

amount_of_correct_answers = amount_of_correct_answers + 1;
amount_of_correct_answers+=1;
amount_of_correct_answers++; // you could use also ++amount_of_correct_answers in this case and have the same result
© www.soinside.com 2019 - 2024. All rights reserved.