gpt4 book ai didi

java - java中如何让一个方法转到main方法?

转载 作者:行者123 更新时间:2023-12-01 13:08:36 24 4
gpt4 key购买 nike

我实际上在 java 中遇到了更多问题 - 就像我将参数放在标准方法中,而不是 main 中,并使其可以返回到 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。

最佳答案

不确定您想通过调用 main 方法来实现什么目的。但您可以按照以下方法执行此操作:

    main(new String[1]);

而不是

   main(String[] args);

Main方法是一个特殊的方法,JVM使用它作为启动应用程序代码的入口点。所以在代码中调用是合法的,但并不可取。我建议您将代码移至不同的方法来显示菜单选项。

关于java - java中如何让一个方法转到main方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23073085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com