开发者

how to create a counter in a dr Java program

开发者 https://www.devze.com 2022-12-08 11:20 出处:网络
/*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.*/
/*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.");
        Syst开发者_高级运维em.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消