【问题标题】:how to create a counter in a dr Java program如何在 dr Java 程序中创建计数器
【发布时间】:2009-10-12 00:10:28
【问题描述】:
/*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");
        }
    }
}

这是我正在编写的程序。我想制作一个计数器来记录每个正确回答的问题,然后在问题完成后将其显示给用户。我试过用这个:

int score=0;

score=score+1;

它不适用于第二个问题,但适用于第三个问题......它给了我一个错误。 还有其他方法可以做到这一点还是我做错了什么?

【问题讨论】:

  • 嘿,非常感谢,我尝试增加它,但我输入的代码错误,所以我一直收到错误,但现在应该可以工作了
  • 啊,不,它说它不起作用文件:K:\Quiz\Quiz.java [行:29] 错误:K:\Quiz\Quiz.java:29:表达式的非法开始请帮助
  • 您需要在语句的末尾包含分号。

标签: java counter


【解决方案1】:

看起来您走在正确的轨道上。您需要在程序开始时声明一个 socre 变量。

int score = 0;

然后在每个打印出“正确”的问题中,您可以像这样增加分数:

 score++;

在最后一道题之后的程序结束时,您可以打印分数。

也许您应该发布您尝试时遇到的错误。

更新: 语法是 score++ 而不是 score=++。即去掉=号。

【讨论】:

    【解决方案2】:

    你所做的是正确的。注意对您帖子的评论;您在发布的解决方案末尾需要分号。此外,根据Java Language Specification,最好使用所有小写字符来命名您的变量:

    int score = 0;
    
    // question code
    
    score += 1;
    
    or 
    
    score = score + 1;
    
    or 
    
    score++;
    

    您需要将变量声明 (int score = 0;) 放在任何循环(您的 if/else 循环)之外。最好放在 main 方法的第一行。

    【讨论】:

      【解决方案3】:

      您的问题可能是因为在比较他们给定的答案时,您在名称“James Naismith”之后有一个空格字符。要使其被评估为真,用户必须使用确切的字符串“James Naismith”而不是“James Naismith”来回答

      编辑:没关系,这不会导致错误,但仍需要引起您的注意,因为它可能会影响程序的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-19
        • 2020-03-08
        • 2014-02-09
        • 2023-04-10
        • 2015-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多