我编译了两个java源码,分别是version.class和open.class
基本上,version.class包含public static void main子句,而open.class主要是一组使整个程序运行的构造函数。
这是我的做法:
if ("n".equalsIgnoreCase(input)) {
new open();
} else if ("q".equalsIgnoreCase(input)) {
System.exit(0);
}
问题是当我在 CMD 中运行它时,我输入了以下内容:
java version
它说该命令无法找到或加载主类。造成这种情况的主要问题是什么?谢谢。我希望它在编译时也能在 CMD 中运行,所以我可以制作一个 bat 文件来使程序也能在 CMD 中运行。 编辑:
这是整个版本类:
import java.util.Scanner;
public class version{
public static String input;
public static Scanner s = new Scanner (System.in);
public static void main (String [] args ) {
System.out.println("========================");
System.out.println("ANAGRAM MASTER VERSION 1.0");
System.out.println("Created by: Janrae Mendoza");
System.out.println("==========================");
System.out.println("Press the corresponding letters for your desired option: ");
System.out.println("<n> New Game");
System.out.println("<q> Quit Game");
input = s.nextLine();
if ("n".equalsIgnoreCase(input)) {
new open();
} else if ("q".equalsIgnoreCase(input)) {
System.exit(0);
}
}
}
虽然这是公开课:
import java.util.Random;
import java.util.Scanner;
public class open{
public String input;
public Scanner s = new Scanner (System.in);
public Random r = new Random();
//at least 25 words
public String scrambled[] = {"MICSECO", "LEPAIM", "ICHORE", "MLORTA", "TIEEXSUQI",
"MEERTO", "DESTOAK", "MLIBOE", "PHRAPARGA", "UALBILNGI", "DOXORTHRO",
"ZEIMESREM", "OKVEPRO", "ILAPRS", "TEMOED", "QUEMSADRAE", "LSEIMUP",
"CREUPRO", "BLRUNDE", "CKRENO", "UIPRSUT", "BINMEHLOGO", "DRKERA",
"SEACEDAP", "SOULEAZ"};
public String scrAns[] = {"ECONOMICS", "IMPALE", "HEROIC", "MORTAL", "EXQUISITE",
"REMOTE", "STOCKADE", "MOBILE", "PARAGRAPH", "BILINGUAL", "ORTHRODOX",
"MESMERIZE", "PROVOKE", "SPIRAL", "DEMOTE", "MASQUERADE", "IMPULSE",
"PROCIRE", "BLUNDER", "RECKON", "PURSUIT", "HEMOGLOBIN", "DARKER",
"ESCAPADE", "ZEALOUS"};
public int word;
public boolean stop = false;
public int scrnum;
public int timesplayed;
public void sleep() {
try {
Thread.sleep(1000);
} catch (Exception e) {}
}
public open() {
while (stop == false) {
sleep();
System.out.println("Guess out the scrambled word!");
word = r.nextInt(24);
scrnum = word;
System.out.println("ANAGRAM: " + scrambled[word]);
System.out.print("YOUR ANSWER: ");
input = s.nextLine();
if (input.equalsIgnoreCase(scrAns[scrnum])) {
System.out.println("You guessed the word right!");
System.out.println("Continue playing? <y> Yes and any other keys for No");
input = s.nextLine();
if ("y".equalsIgnoreCase(input)) {
timesplayed++;
} else {
stop = true;
}
}
}
System.out.println("Thank you for trying out version 1 of this game! You played " + timesplayed
+ " times! Enjoy your day, player!");
}
}
最佳答案
根据您的问题,我可以看到您创建了两个类,它们是
- 版本.class
- 开放类
你从哪里获得“titleScreen”类?
您仅在 version.class 中有 main 方法。 所以,version.class的main()方法只能作为入口点。
您可以像下面这样运行该程序:
java版本标题屏幕
titleScreen可以作为该程序的输入。
关于java - 运行已编译的 Java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9647954/