我是Java和一般编程人员的新手。我正在使用NetBeans IDE处理以下代码:

package ShoutBox;
import java.util.Scanner;


public class ShoutBox {
    public static void main(String args[]){
    ShoutBox shoutbox = new ShoutBox();
    }
    Scanner input = new Scanner(System.in);
    int selection = 0;


    public void message() {

        String message[] = new String[10]; // declare array

        message[0] = "01: Free Will Exists.";
        message[1] = "02: Since Free Will Exists, Evil Exists.";
        message[2] = "03: Evil, being evil, will not stop on its own.";
        message[3] = "04: For Good to continue to exist, Evil must be stopped.";
        message[4] = "05: It is good that Good exists.";
        message[5] = "06: It is good to stop evil.";
        message[6] = "07: Good persons will attempt to stop evil or else they are not good.";
        message[7] = "08: Evil brings war.";
        message[8] = "09: Since evil brings war and will not stop, good must fight and win those wars.";
        message[9] = "10: Just war exists.";

        System.out.println();

        String cm = new ShoutBox().shoutOutCannedMessage(message); //call to shoutOutCannedMessage
        String rm = new ShoutBox().shoutOutRandomMessage(message);
        System.out.println("Philosophy: ");
        System.out.println(cm);
        System.out.println("Your Random Message: ");
        System.out.println(rm);


    }

    public String shoutOutCannedMessage(String[] message)
    {
    ShoutBox shoutbox = new ShoutBox();
        for (String element : message)
        {
            System.out.println(element); //print out messages
        }

        System.out.print("Select a message: ");
        System.out.println();

        selection = input.nextInt();

        String cannedMessage = message[selection];

        return cannedMessage;


    }


}

它可以很好地构建和加载,但是不会运行,并显示错误代码:“错误:找不到或加载主类shoutbox.ShoutBox。”有人知道为什么是这样吗?

最佳答案

它不运行的原因是因为Java(和类名)中的程序包区分大小写。您应该改为将包声明为package shoutbox

通过命名约定,您应该始终以小​​写形式命名您的程序包(表示程序包的文件夹也应如此),并且您始终应使用大写的类名。

09-25 20:33