我实际上在java中遇到了更多的问题 - 就像我在标准方法中使用参数而不是主要方法,并使其可以返回到main方法。这是我实际执行的代码:
import java.util.Scanner;
public class boxtype {
public static void main(String args[]) {
String[] melee = {"Crowbar", "Bowie Knife", "Butterfly Knife", "Knuckleduster"};
String[] pistol = {"Colt .22", "Magnum .45", "P250", "9mm Pistol"};
String[] assault = {"AK47", "M4A1", "M16", "SMG", "Mac10", "Minigun (HGE)"};
String[] shotgunsniper = {"Shotgun", "Benelli S90", "Sniper Rifle"};
String[] attachments = {"Laser Sight", "Silencer", "Scope", "Auto-target"};
System.out.println("TypeClass loader - type 'help' for help");
Scanner scan = new Scanner(System.in);
String xy = scan.nextLine();
if (xy.equals("spyclass")) {
spyClass(assault, attachments, pistol, melee);
}
}
private static void spyClass(String[] assault, String[] pistol, String[] attachments, String[] melee) {
System.out.println("Spy class: ");
System.out.println("Primary weapon: " + assault[1] + " + " + attachments[1]);
System.out.println("Secondary weapon: " + pistol[0] + " + " + attachments[1]);
System.out.println("Melee Weapon: " + melee[2]);
}
private static void helpeur() {
System.out.println("spyclass - goes to the spy class");
Scanner scan = new Scanner(System.in);
String xy = scan.nextLine();
if (xy.equals("main menu")) {
main(String[] args);
}
}
}
但它说“String args []不是声明的变量”,我运行Eclipse。
答案 0 :(得分:1)
通过调用main方法不确定您要实现的目标。但是你可以这样做:
main(new String[1]);
而不是
main(String[] args);
主方法是一种特殊方法,JVM使用它作为启动应用程序代码的入口点。所以调用代码是合法的,但不可取。我建议您将代码移动到另一种方法来显示菜单选项。
答案 1 :(得分:0)
你不能(好吧,也许你可以,但你不应该)调用程序中的main方法。而是考虑添加某种start()
方法,其中包含main方法中的代码。然后你可以这样做:
if (xy.equals("main menu")) {
start();
}
顺便说一下,你收到错误的原因是因为
String[] args
没有值,因此无法作为参数传递。