how to create a counter in a dr Java program

筅森魡賤 提交于 2019-12-02 07:16:36

It looks like you are on the right track. You need to declare a socre variable at the begiunning of the program.

int score = 0;

Then in each question where you print out "correct" you can increment the score like this:

 score++;

At the end of the program after the last question you can print the score.

Maybe you should post the error you got when you tried it.

UPDATE: The syntax is score++ NOT score=++. That is, take out the = sign.

What you did is correct. Heed the comment on your post; you need semi-colons at the end of your posted solution. Also, per the Java Language Specification, it's best to name your variable with all lower case characters:

int score = 0;

// question code

score += 1;

or 

score = score + 1;

or 

score++;

You need to place the variable declaration (int score = 0;) outside of any loops (your if/else loops). It would be best to place it at the first line of the main method.

Your problem is possible because you have a whitespace character after the name "James Naismith" in the comparison for their given answer. For it to be evaluated to true the user must answer with the exact string "James Naismith " instead of "James Naismith"

Edit: Nevermind, This should not cause an error but it is something to bring your attention to still because it could affect the result of the program.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!