Java 将仅使用构造函数的程序转换为使用Main方法的程序
我的老师给我的作业评分有困难。我用BlueJ创建了以下程序,他很可能使用Eclipse。问题是我无法让BlueJ使用main方法启动任何代码,所以我选择使用构造函数。将我的BlueJ代码复制/粘贴到Eclipse中会显示出问题:Java 将仅使用构造函数的程序转换为使用Main方法的程序,java,constructor,Java,Constructor,我的老师给我的作业评分有困难。我用BlueJ创建了以下程序,他很可能使用Eclipse。问题是我无法让BlueJ使用main方法启动任何代码,所以我选择使用构造函数。将我的BlueJ代码复制/粘贴到Eclipse中会显示出问题: Error: Could not find or load main class Misspelled 如何将下面的仅构造函数程序转换为具有有效main方法的程序?我不再需要构造函数了 import java.util.Scanner; 公共类随机游戏 { int
Error: Could not find or load main class Misspelled
如何将下面的仅构造函数程序转换为具有有效main方法的程序?我不再需要构造函数了
import java.util.Scanner;
公共类随机游戏
{
int usersScore=0;
公共游戏()
{
扫描仪用户输入=新扫描仪(System.in);
字符串[]错误的单词={“错贴”、“科布拉”、“威斯福”、“地址”、“可更改”、“独立”、“灰烬”、“cieling”、“humerous”、“wierd”};
String[]rightWords={“北极”、“杂”、“块”、“偏见”、“感激”、“狂喜”、“着迷”、“确定”、“多变”、“有意识”};
双随机数;
int随机数rint;
字符串字检查;
//显示规则
System.out.printf(“输入'y'或'n'”);
//继续循环游戏
while(usersScore<5)
{
//生成一个随机数
randomNumber=GeneratorAndomNumber();//Math.round(10*Math.random());
randomNumberInt=((int)(randomNumber));
//显示用户的分数
System.out.printf(“\n\n当前分数:%d\n”,usersCore);
//检查数组中随机数的位置值
如果(随机数%2==0)
{
wordToCheck=rightWords[RandomNumberPrint];
System.out.printf(“正确吗?:%s\n”,wordToCheck);
rightCheck(userInput.next());
}
其他的
{
wordToCheck=错误的单词[RandomNumberPrint];
System.out.printf(“正确吗?:%s\n”,wordToCheck);
错误检查(userInput.next());
}
}
//System.out.printf(wordToCheck);
}
公共双生成器域编号()
{
双随机数;
randNum=Math.round(10*Math.random());
返回(randNum);
}
公共布尔右校验(字符串usersAnswer)
{
if(usersAnswer.equals(“y”))
{
System.out.printf(“正确!来自rightCheck”);
UsersCore++;
返回(真);
}
其他的
{
System.out.printf(“rightCheck不正确”);
用户帐户--;
返回(假);
}
}
公共布尔错误检查(字符串usersAnswer)
{
if(usersAnswer.equals(“n”))
{
System.out.printf(“正确!来自错误检查”);
UsersCore++;
返回(真);
}
其他的
{
System.out.printf(“错误检查导致的错误”);
用户帐户--;
返回(假);
}
}
}
Writepublicstaticvoidmain(字符串args[])
在main中,创建一个类的实例。一种方法是创建另一个类来保存main方法
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();
}
考虑以下步骤:
publicstaticvoidmain(String[]args)
static
方法usersScore
更改为静态变量,要么创建RandomGame的实例而不是main来跟踪usersScore
以下代码进行了以下更改:
- 向主方法添加了构造函数逻辑
- 使方法保持静态
- 使磁场静止
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); } } }
import java.util.Scanner; 公共类随机游戏{ 静态int usersScore=0; 公共静态void main(字符串[]args){ 扫描仪用户输入=新扫描仪(System.in); 字符串[]错误单词={“错贴”、“科布拉”、“愿望”、“地址”, “易变”、“独立”、“灰烬”、“雪岭”、“肱骨”, “wierd”}; String[]rightWords={“北极”、“杂项”、“片段”, “偏见”、“感激”、“狂喜”、“迷恋”、“确定”, “多变的”、“有意识的”}; 双随机数; int随机数rint; 字符串字检查; //显示规则 System.out.printf(“输入'y'或'n'”); //继续循环游戏 while(usersScore<5){ //生成一个随机数 randomNumber=generateRandomNumber();//Math.round(10* //Math.random()); randomNumberInt=((int)(randomNumber)); //显示用户的分数 System.out.printf(“\n\n当前分数:%d\n”,usersCore); //检查数组中随机数的位置值 如果(随机数%2==0){ wordToCheck=rightWords[RandomNumberPrint]; System.out.printf(“正确吗?:%s\n”,wordToCheck); rightCheck(userInput.next()); }否则{ wordToCheck=错误的单词[RandomNumberPrint]; System.out.printf(“正确吗?:%s\n”,wordToCheck); 错误检查(userInput.next()); } } //System.out.printf(wordToCheck); } 公共静态双生成器domNumber(){ 双随机数; randNum=Math.round(10*Math.random()); 返回(randNum); } 公共静态布尔rightCheck(字符串usersAnswer){ if(usersAnswer.equals(“y”)){ System.out.printf(“正确!来自rightCheck”); UsersCore++; 返回(真); }否则{ System.out.printf(“rightCheck不正确”); 用户帐户--; 返回(假); } } 公共静态布尔错误检查(字符串u)
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); } } }