有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java分数变量不用于数学游戏程序

我创建了一个数学游戏,随机问十个问题,最后给你打分。我创建了一个名为score的整数变量,并将其初始化为0。在if声明中,如果回答正确,将获得10分。我想既然我在for循环中的分数是+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


共 (2) 个答案

  1. # 1 楼答案

    你会遇到问题,因为你在for循环内部初始化变量,然后试图在外部使用它,这是不可能的,因为语言就是这样产生的。这被称为scope。您应该知道,变量仅在已声明的范围内(括号中)是活动的。就像在您的例子中一样,它只在for循环中可用,然后由垃圾收集器收集。这并不严重,只是在实现for循环之前定义score变量

    int score;
    
    for(int i...)
    {
        // ...
    }
    
    // now you can use score here:
    

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

  2. # 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);
    
    
        }
    }