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