运行 Java 文件时没有此类方法错误

no such method error when running java file

提问人:dawsUTV 提问时间:10/13/2023 最后编辑:Jeevan ebidawsUTV 更新时间:10/14/2023 访问量:43

问:

我一直在从 python 迁移到 java,所以仍然习惯了 OOP。我试图制作一个游戏,它会生成 2 个随机数,用户必须将它们加在一起。

但是,当我运行游戏时,我收到一个意外的错误代码:

线程“main”中的异常 java.lang.NoSuchMethodError: 'void Game.playGame()' 在 additionGame.main(additionGame.java:8)

我在下面添加了我的代码:

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

public class AdditionGame {
    public static void main(String[] args){

        Game gameObject = new Game();
        gameObject.playGame();

    }
}

class Game {
    int score = 0;

    public int generateNumber(){
        int number;
        Random random = new Random();
        number = random.nextInt(20)+1;
        return number;
    }

    public void playGame(){
        int i = 0;
        while (i<10){
            int number1;
            int number2;
            int answer;
            String userAnswerSTR;
            int userAnswer;

            i++;

            number1 = generateNumber();
            number2 = generateNumber();

            answer = number1 + number2;

            System.out.println("What is " + number1 + "+" + number2 + " :");
            Scanner scannerObject = new Scanner(System.in);
            userAnswerSTR = scannerObject.nextLine();
            scannerObject.close();

            userAnswer = Integer.parseInt(userAnswerSTR);

            if (userAnswer == answer){
                System.out.println("Correct Answer!");
                score ++;
            }

            else {
                System.out.println("Incorrect! The answer was: " + answer);
            }

        }
    System.out.println("Congratulations! Your score was: " + score);
    }

}

我预计游戏会运行 10 次,然后打印用户分数

Java 方法 nosuchmethoderror

评论

2赞 Jesper 10/13/2023
您是否正确编译了源代码并且没有编译错误?这听起来像是您的计算机上的类的编译版本已过时。Game
1赞 Arnaud 10/13/2023
同样按照惯例,类的名称应以大写字母开头,例如AdditionGame
0赞 redwards 10/13/2023
正确。无法重现 NoSuchMethodError

答:

0赞 Jeevan ebi 10/13/2023 #1

这是您可以尝试的 approch :

我预计游戏会运行 10 次,然后打印用户分数

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

public class AdditionGame {
    public static void main(String[] args) {
        Game game = new Game();
        game.playGame();
    }
}

class Game {
    int score = 0;

    public int generateNumber() {
        Random random = new Random();
        return random.nextInt(20) + 1;
    }

    public void playGame() {
        Scanner scanner = new Scanner(System.in);

        for (int i = 0; i < 10; i++) {
            int number1 = generateNumber();
            int number2 = generateNumber();
            int answer = number1 + number2;

            System.out.print("What is " + number1 + " + " + number2 + " : ");
            int userAnswer = scanner.nextInt();

            if (userAnswer == answer) {
                System.out.println("Correct Answer!");
                score++;
            } else {
                System.out.println("Incorrect! The answer was: " + answer);
            }
        }

        System.out.println("Congratulations! Your score was: " + score);

        scanner.close();
    }
}

评论

1赞 Jesper 10/13/2023
但问题是关于一个完全不同的错误:而不是.NoSuchMethodErrorNoSuchElementException
0赞 Jeevan ebi 10/13/2023
@Jesper,感谢您的纠正,我已经提到了一些其他方法来满足他的要求!