java - 将仅包含构造函数的程序转换为包含 main 方法的程序

标签 java constructor

我的老师在给我的作业评分时遇到问题。我在 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);
    }
}
}

最佳答案

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

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();
    }

关于java - 将仅包含构造函数的程序转换为包含 main 方法的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15198520/

相关文章:

java - 将 @Provides 方法绑定(bind)为 eager singleton

java - Java中忽略CSV文件中的空白行

c++ - 当我们开始覆盖内存时,对象的生命周期已经结束了吗?

ruby - 如何在不使用 new 的情况下在 Ruby 中创建对象

java - 有没有办法让netbeans使用热点服务器虚拟机

java - JAXB 的第一步 - 抽象类和子类型

java - Eclipse dsl 应用程序无法隐藏未使用的透视图

c# - 在 C# 中是否可以强制只能从构造函数调用私有(private)函数?

c++ - vector<T> 调用的构造函数的奇怪问题

c# - 构造函数中的虚拟成员调用