0

我的老师在给我的作业评分时遇到问题。我在 BlueJ 中创建了以下程序,他很可能使用 Eclipse。问题是我无法让 BlueJ 使用 main 方法启动任何代码,所以我只是选择使用构造函数。将我的 BlueJ 代码复制/粘贴到 Eclipse 中表明它存在问题:

Error: Could not find or load main class Misspelled

如何将下面的仅限构造函数的程序转换为具有有效 main 方法的程序?我不再需要构造函数了。


import java.util.Scanner;


public class RandomGame
{
int usersScore = 0;

public RandomGame()
{
    Scanner userInput = new Scanner(System.in);

    String[] wrongWords = {"mispelled","kobra", "wishfull", "adress", "changable","independant", "emberrass", "cieling", "humerous", "wierd"} ;
    String[] rightWords = {"arctic", "miscellaneous", "piece", "prejudice", "grateful","ecstasy", "fascinate", "definite", "changeable", "conscious"};
    double randomNumber;
    int randomNumberInt;
    String wordToCheck;



            //Display Rules
    System.out.printf("Enter Either 'y' or 'n'");

            //Keep Looping Game
    while(usersScore < 5)
    {
            //Generate A Random Number
    randomNumber = generateRandomNumber();     //Math.round(10 * Math.random());
    randomNumberInt = ((int)(randomNumber));


            //Display User's Score
    System.out.printf("\n\nCurrent Score: %d\n", usersScore);

            //Check Place Value Of Random Number In Array
    if(randomNumber %2 == 0)
    {
        wordToCheck = rightWords[randomNumberInt];

        System.out.printf("Correct?: %s\n", wordToCheck);
        rightCheck(userInput.next());
    }
    else
    {
        wordToCheck = wrongWords[randomNumberInt];

        System.out.printf("Correct?: %s\n", wordToCheck);
        wrongCheck(userInput.next());
    }

   }
    //System.out.printf(wordToCheck);



}

public double generateRandomNumber()
{
    double randNum;

    randNum = Math.round(10 * Math.random());

    return(randNum);
}

public boolean rightCheck(String usersAnswer)
{
    if(usersAnswer.equals("y"))
    {
        System.out.printf("Correct! from rightCheck");
        usersScore++;

        return(true);
    }
    else
    {
        System.out.printf("Incorrect from rightCheck");
        usersScore--;

        return(false);
    }
}

public boolean wrongCheck(String usersAnswer)
{
    if(usersAnswer.equals("n"))
    {
        System.out.printf("Correct! from wrongCheck");
        usersScore++;

        return(true);
    }
    else
    {
        System.out.printf("Incorrect from wrongCheck");
        usersScore--;

        return(false);
    }
}
}
4

4 回答 4

1

一种方法是创建另一个类来保存您的主要方法

public class Main
{
    public static void main (String args[])
    {
        RandomGame rGame = new RandomGame();
    }
}

(注意:上述情况下.java文件的名字必须是Main.java)

否则,您可以通过以下方式将 main() 添加到您的 RandomGame 类

public static void main (String args[])
    {
        RandomGame rGame = new RandomGame();
    }
于 2013-03-04T09:45:55.097 回答
1

考虑以下步骤:

  1. 将构造函数更改为 main 方法public static void main(String[] args)
  2. 将构造函数中使用的所有私有/公共方法更改为static方法
  3. 要么将成员变量更改usersScore为静态,要么创建 RandomGame 实例而不是 main 来跟踪usersScore

给一个人一条鱼,你就喂他一天;教他怎么钓鱼,你养他一辈子

于 2013-03-04T09:48:12.860 回答
0

public static void main(String args[])

在 main 里面,创建一个你的类的实例。

于 2013-03-04T09:45:43.597 回答
0

下面的代码进行了以下更改:

  • 向 main 方法添加了构造函数逻辑
  • 使方法静态
  • 使字段静态

    import java.util.Scanner;
    
    public class RandomGame {
        static int usersScore = 0;
    
        public static void main(String[] args) {
            Scanner userInput = new Scanner(System.in);
    
            String[] wrongWords = { "mispelled", "kobra", "wishfull", "adress",
                    "changable", "independant", "emberrass", "cieling", "humerous",
                    "wierd" };
            String[] rightWords = { "arctic", "miscellaneous", "piece",
                    "prejudice", "grateful", "ecstasy", "fascinate", "definite",
                    "changeable", "conscious" };
            double randomNumber;
            int randomNumberInt;
            String wordToCheck;
    
            // Display Rules
            System.out.printf("Enter Either 'y' or 'n'");
    
            // Keep Looping Game
            while (usersScore < 5) {
                // Generate A Random Number
                randomNumber = generateRandomNumber(); // Math.round(10 *
                                                        // Math.random());
                randomNumberInt = ((int) (randomNumber));
    
                // Display User's Score
                System.out.printf("\n\nCurrent Score: %d\n", usersScore);
    
                // Check Place Value Of Random Number In Array
                if (randomNumber % 2 == 0) {
                    wordToCheck = rightWords[randomNumberInt];
    
                    System.out.printf("Correct?: %s\n", wordToCheck);
                    rightCheck(userInput.next());
                } else {
                    wordToCheck = wrongWords[randomNumberInt];
    
                    System.out.printf("Correct?: %s\n", wordToCheck);
                    wrongCheck(userInput.next());
                }
    
            }
            // System.out.printf(wordToCheck);
    
        }
    
        public static double generateRandomNumber() {
            double randNum;
    
            randNum = Math.round(10 * Math.random());
    
            return (randNum);
        }
    
        public static boolean rightCheck(String usersAnswer) {
            if (usersAnswer.equals("y")) {
                System.out.printf("Correct! from rightCheck");
                usersScore++;
    
                return (true);
            } else {
                System.out.printf("Incorrect from rightCheck");
                usersScore--;
    
                return (false);
            }
        }
    
        public static boolean wrongCheck(String usersAnswer) {
            if (usersAnswer.equals("n")) {
                System.out.printf("Correct! from wrongCheck");
                usersScore++;
    
                return (true);
            } else {
                System.out.printf("Incorrect from wrongCheck");
                usersScore--;
    
                return (false);
            }
        }
    }
    
于 2013-03-04T09:48:21.890 回答