【问题标题】:Score variable is not used in math game program数学游戏程序中不使用分数变量
【发布时间】:2014-05-24 16:35:03
【问题描述】:

我创建了一个数学游戏,它会问十个随机问题并在最后给你打分。我创建了一个名为 score 的整数变量并将其初始化为 0。在 if 语句中,如果您回答正确,您将获得 10 分。我想既然我在 for 循环中有 score + 10 ,那么我不应该为评分系统使用增量。如果有人能指出我为什么 Java 告诉我没有使用 score 变量的正确方向,我将不胜感激。来自德克萨斯州的欢呼声!

package pkgnew;

import java.util.Scanner;
import java.util.Random;

public class New {

    public static void main(String args[]) {

        //Game 10x loop        
        for(int i = 0; i < 10; i++)
        {

        //Declare and construct variables 
        Random randomnum = new Random();
        int fnum, snum;
        int mathnumber = randomnum.nextInt(20);
        int newnumber = randomnum.nextInt(20);

        //Declare and construct variables of math problem
        fnum = mathnumber;
        snum = newnumber;

        //Declare random operator variable
        String[] operators = {"+" , "-", "*", "/" };
        int randomIndex = randomnum.nextInt(3);
        String symbol = operators[randomIndex];

        //Initialize answer and score
        int answer = 0;
        int score = 0;

        //Switch statement for random operator and question display
        switch (symbol) {
            case "+": 
                System.out.println(fnum + "+" + snum);
                answer = fnum+snum;
                break;
            case "-":
                System.out.println(fnum + "-" + snum);
                answer = fnum-snum;
                break;
            case "*":
                System.out.println(fnum + "*" + snum);
                answer = fnum*snum;
                break;
            case "/":
                System.out.println(fnum + "/" + snum);
                answer= fnum/snum;
                break;
        }

        //User input
        Scanner serena = new Scanner(System.in);
        int userAnswer = serena.nextInt();

        //If user input = answer display "correct"
        if (userAnswer == answer) {
        System.out.println("Correct!");
        score = + 10;

        //If user input does not = answer display "wrong" and correct answer
        } else {
        System.out.print("Wrong! The correct answer is: " );
        System.out.println(answer);
        }

        }

        System.out.println("Game Over!");
        System.out.println("Your score is:");
        System.out.println(score);


    }
}

我使用 Java 8 和 NetBeans 8.0。

【问题讨论】:

  • 分数 = + 10;这甚至可以编译吗?
  • 它可以编译,但不能满足他的需要。这只是使数字为正并将其分配给变量,但这基本上是没有意义的,因为数字总是为正的。只需说出score += 10 即可修复它。你应该知道这和score = score + 10是一样的,只是一个提高可读性的语法糖。
  • 谢谢山猫!我修复了语法。在显示分数的最后一行,Java 说它找不到变量 score。在第 31 行,当 score 被初始化时,它说它从未被使用过。我感觉我可能没有正确使用该变量。
  • 编辑了我的答案。这是一个简单的范围问题
  • 问题是最后,它不会打印最终分数,因为它说变量没有被使用。如果这是我的 IDE 的问题,我该如何解决?

标签: java variables


【解决方案1】:

您会遇到问题,因为您在 for 循环内初始化变量,然后尝试在外部使用它,这是不可能的,因为这就是语言的制作方式。这称为scope。而且您应该知道,变量仅在已声明的范围内(在括号中)是有效的。就像您的情况一样,它仅在 for 循环中可用,然后由垃圾收集器收集。没什么大不了的,只是在执行for循环之前定义分数变量。

int score;

for(int i...)
{
    // ...
}

// now you can use score here:

还有一件事:你根本没有增加你的分数变量。你只是说score = +10。这只是将正数设为 10,正数,将其分配给变量,并且无论变量 score 具有什么数据,它都不再具有它,这不是您想要做的。我认为您想要使用的正确方法是使用 += 运算符,如下所示:score += 10;。 += 是一种语法糖,用于提高可读性,其作用与 score = score + 10; 完全相同。

【讨论】:

  • 你仍然需要在循环之前初始化score=0;。 (并且变量将分配在堆栈上,而不是堆上,因此垃圾收集器与它的销毁无关)
  • 如果您不指定整数的初始值,它将为零,因为这是一种值数据类型。每个值数据类型都有其默认值(例如 bool - false、double - 0 等)。第二件事是不能在堆上分配像整数这样的值类型。无论如何,它总是保存在堆栈中。你理解的有点不对。谷歌价值与参考数据类型的更多信息。
  • 在 Java 中只有字段变量被赋予默认值,局部变量没有,参见stackoverflow.com/questions/9687634/java-variable-default-value。您同意 score 将在堆栈上分配,因此垃圾收集器不会释放它,请参阅 stackoverflow.com/questions/2447504/…
【解决方案2】:

您的问题是范围。如果你在循环内定义了一个变量,那么循环外的任何东西都看不到它。

package pkgnew;

import java.util.Scanner;
import java.util.Random;

public class New {

    public static void main(String args[]) {


        //Score must go here, outside loop
        int score = 0;

        //Game 10x loop        
        for(int i = 0; i < 10; i++)
        {
            //Logic

        }
        System.out.println("Game Over!");
        System.out.println("Your score is:");
        //Otherwise score is not defined here
        System.out.println(score);


    }
}

【讨论】:

  • 谢谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多