没有检测到主要方法

时间:2014-10-28 12:38:19

标签: java class main

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

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。

1 个答案:

答案 0 :(得分:0)

该计划没有任何问题。

我做了以下事情:

[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方法。编译器无法知道你是否这样做,所以它不能抱怨它。

  • "Hello World!" for the NetBeans IDE页面将指导您完成使用NetBeans编译和运行简单程序的步骤。也许这会给你一些关于你做错了什么的线索。