未检测到主要方法



编译器一直说没有主方法,但有一个:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class StarWarsFinal
{
    final static Map<String, String> firstNameMap = new HashMap<>();
    static {
    firstNameMap.put("A", "Cho");
    firstNameMap.put("B", "R2");
    firstNameMap.put("C", "C-3po");
    firstNameMap.put("D", "Yod");
    firstNameMap.put("E", "Nas");
    firstNameMap.put("F", "Slea");
    firstNameMap.put("G", "Jan");
    firstNameMap.put("H", "Zhur");
    firstNameMap.put("I", "Boba");
    firstNameMap.put("J", "Thre");
    firstNameMap.put("K", "Bib");
    firstNameMap.put("L", "Kit");
    firstNameMap.put("M", "Kyp");
    firstNameMap.put("N", "Gonk");
    firstNameMap.put("O", "Zlung");
    firstNameMap.put("P", "Adi");
    firstNameMap.put("Q", "Nat");
    firstNameMap.put("R", "Ru");
    firstNameMap.put("S", "Cla");
    firstNameMap.put("T", "Kir");
    firstNameMap.put("U", "Obi");
    firstNameMap.put("V", "Ken");
    firstNameMap.put("W", "Ziro");
    firstNameMap.put("X", "Zev");
    firstNameMap.put("Y", "Tavion");
    firstNameMap.put("Z", "Jar");
    }

    final static Map<String, String> lastNameMap = new HashMap<>();
    static {
    lastNameMap.put("A", "tzki");
    lastNameMap.put("B", "hut");
    lastNameMap.put("C", "der");
    lastNameMap.put("D", "kzos");
    lastNameMap.put("E", "vos");
    lastNameMap.put("F", "vader");
    lastNameMap.put("G", "thrawn");
    lastNameMap.put("H", "mesk");
    lastNameMap.put("I", "thuo");
    lastNameMap.put("J", "skywalker");
    lastNameMap.put("K", "D2");
    lastNameMap.put("L", "maul");
    lastNameMap.put("M", "sith");
    lastNameMap.put("N", "muzzar");
    lastNameMap.put("O", "jusik");
    lastNameMap.put("P", "horn");
    lastNameMap.put("Q", "phisto");
    lastNameMap.put("R", "farlander");
    lastNameMap.put("S", "dunhaussan");
    lastNameMap.put("T", "jar");
    lastNameMap.put("U", "binks");
    lastNameMap.put("V", "lbis");
    lastNameMap.put("W", "gnarzlo");
    lastNameMap.put("X",  "anakin");
    lastNameMap.put("Y",  "ackbur");
    lastNameMap.put("Z", "axmis"); 
    }
    final static Map<String, String> jobMap = new HashMap<>();
    static{
    jobMap.put("Jan","Clone");
    jobMap.put("Feb","Bounty Hunter");
    jobMap.put("Mar","Droid");
    jobMap.put("Apr","Jedi Knight");
    jobMap.put("May","Gungan");
    jobMap.put("Jun","Gangster");
    jobMap.put("Jul","commander");
    jobMap.put("Aug","ewok");
    jobMap.put("Sep","Queen");
    jobMap.put("Oct","Empirer");
    jobMap.put("Nov","Darth");
    jobMap.put("Dec","captain");

    }
  public static void main ( String[] args )
  {
      String[] planet = null, rank = null, rebbelion = null, letter1 = null, letter2= null , Map , HashMap;

     Scanner input = new Scanner( System.in ); //scanner initilized

    planet = new String[11]; //Planet options  
    planet[0] = "Alderaan";
    planet[1] = "Bespin";
    planet[2] = "Coruscant";
    planet[3]= "Forest moon of Endor";
    planet[4] = "Hoth";
    planet[5] = "Kamino";
    planet[6] = "Kashyyk";
    planet[7] = "Mustafar";
    planet[8] = "Yavin";
    planet[9] = "DEATH STAR";//Planet options  -END


        System.out.println("Welcome to the Star Wars name generator");
        System.out.println("What is your first name?");     //Name Generation
        String firstName = input.nextLine();
        String newFirst = firstNameMap.get(firstName.toUpperCase().substring(0,1));         //Name Generation (i want to take the first letter of there input and get the output from letter1 matching that # ie c = letter1 [2]  a= letter1 [0])
        System.out.println("What is your last name?");      //Name Generation
        String lastName = input.nextLine(); //Name Generation (i want to take the first letter of there input and get the output from letter2 matching that # ie c = letter2 [2]  a= letter2 [0])
        String newLast = lastNameMap.get (lastName.toUpperCase().substring(0,1));
        System.out.println("What is your Birth month(first 3 letters)?");     //Name Generation
        String month = input.nextLine();
        String job = jobMap.get(firstName.toUpperCase().substring(0,3));

    System.out.println("If you had to choose 1)dark or 2)light side? please input the number"); //Selection of Dark or Light side
    int side = input.nextInt();
                                       //Selection of Dark or Light side
      System.out.println("There are now several places you could live please choose one of the following by number:");    //Planet selections
                System.out.println("1) Alderaan 2) Bespin 3) Coruscant 4) Forest moon of Endor 5) Hoth ");                          //Planet selections
                System.out.println("6) Kamino 7) Kashyyk 8) Mustafar 9) Yavin 10)DEATHSTAR");                                       //Planet selections
                String location =input.nextLine();

        if (side == 1)
        {
            System.out.println("You Have chosen to be part of the dark side!");
                System.out.println("You "+ newFirst + newLast +" now fight for the dark side  As a proud "+ job +"of the"+ location +"Good luck against your enemy's and may the force be with you");
        }

        else
        {
            System.out.println("You are now part of the light side!");
            System.out.println("You "+ newFirst + newLast +" now fight for the light side  As a proud "+ job +"of the"+ location +"Good luck against your enemy's and may the force be with you");
        } 

        System.out.println("Thank you for doing the starwars name generator!");
  }
}

我不知道为什么它不读,因为它早些时候就起作用了。我正在使用NetBeans作为IDE。

程序没有任何问题。

我做了以下事情:

[steve@newbox tmp]$ cat > StarWarsFinal.java
<paste-program-from-question>
[steve@newbox tmp]$ javac StarWarsFinal.java 
[steve@newbox tmp]$ java StarWarsFinal 
Welcome to the Star Wars name generator
What is your first name?
^D
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)
    at StarWarsFinal.main(StarWarsFinal.java:112)
[steve@newbox tmp]$ 

从这一点我们必须得出结论,你正在经历的问题是以下之一:

  • 你没有正确编译它
  • 你没有正确运行它
  • 在NetBeans决定类是否可运行的方式中,存在一些非标准。(这极不可能…IMO)
  • 也许你正在做其他你没有提到的事情

备案:

  • 类没有必要声明为public才能运行,至少当您使用java命令从命令行运行它时是这样。

  • 没有主方法不是Java编译错误。这是一个运行时错误。如果你要尝试运行一个仅的类,它需要一个main方法。编译器无法知道你是否要这样做,所以它不能抱怨。

  • NetBeans IDE页面的"Hello World!"将引导您完成使用NetBeans编译和运行简单程序的步骤。也许这会给你一些线索,让你知道自己做错了什么。

相关内容

  • 没有找到相关文章

最新更新