本文介绍了如何在Java程序中
创建一个计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
/*This is a quiz program that will ask the user 10 questions. the user will answer
* these questions and will be scored out of 10.*/
class Quiz {
public static void main(String args[]) {
// Instructions
System.out.println("instructions");
System.out.println(" ");
System.out
.println("1. You wll be asked ten questions through out the quiz.");
System.out
.println("2. The first question will appear, you will have to answer that question for the next question to appear.");
System.out
.println("3. When you answer the last question you will be told your score.");
System.out.println(" ");
System.out.println("welcome to the basketball quiz.");
// question 1
System.out.println(" ");
System.out.println("Question 1. ");
System.out.println("How tall is a basketball hoop? ");
System.out.println("Type in Answer here:");
String Question1 = In.getString();
if (Question1.equalsIgnoreCase("10 Feet")) {
System.out.println("Correct!");
} else {
System.out.println("you got this questions wrong");
}
// question 2
System.out.println(" ");
System.out.println("Question 2. ");
System.out.println("Who invented basketball? ");
System.out.println("Type in Answer here:");
String Question2 = In.getString();
if (Question2.equalsIgnoreCase("James Naismith ")) {
System.out.println("Correct!");
} else {
System.out.println("you got this questions wrong");
}
}
}
这是我的程序,我我在写我想制作一个计数器,用于记录每个问题的得分,然后在问题结束后将其显示给用户。我尝试使用这个:
This is my program that I am writing. I want to make a counter that will keep score of every question that is answered right and then display it to the user after the questions are finished. I tried using this:
int score=0;
score=score+1;
它不适用于第二个问题,但适用于第三个问题...它给出了我错了。 还有其他方法可以做到这一点,还是我做错了什么?
It doesn't not work for the 2nd question, but works for the 3rd... it gives me an error.
Is there another way I can do this or am I doing something wrong?
推荐答案
看起来你在正确的轨道。你需要在程序开始时声明一个socre变量。
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.
更新: 语法为score ++ NOT score = ++。也就是说,取出=符号。
UPDATE:
The syntax is score++ NOT score=++. That is, take out the = sign.
发布评论