Can't figure out what's causing this error "This class does not have a static void main method accepting String[]."

huangapple go评论74阅读模式
英文:

Can't figure out what's causing this error "This class does not have a static void main method accepting String[]."

问题

我有一个大学作业要在Java中练习测试方法但我弄不明白为什么会出现错误:“此类没有接受String[]的静态void main方法。” 我正在使用Dr. Java另一个需要注意的是我对编程非常新手所以解决方案可能相当明显

主类文件

    public static void displayLine() //显示行
    {
        System.out.println("———————"); //打印线
    }
    
    public static void displayMessage(String message) //显示消息
    {
        System.out.println(message); //显示消息
    }
    
    public static int sumNumbers(int num1, int num2) //用于返回总和的方法
    {
      return (num1 + num2); //将两个数字相加
    }
    
    public static boolean isGreater(int num1, int num2) //查看数字是否更大。
    {
      boolean isGreater; 
      
       if (num1 > num2) //条件语句,查看哪个数字更大
       {
         isGreater = true;
       } else {
           isGreater = false;
         }
         
       return isGreater; //返回是否更大
    }
     
    public static void setBulb(boolean state) //确定灯泡是开还是关
    {
      
        if (state == false) //条件语句,显示灯泡是开还是关。
        {
          System.out.println("灯泡现在关闭");
        } 
        else 
        {
            System.out.println("灯泡现在打开");
        }
          //^^^我无法让这个工作。我想知道是否因为我没有创建方法的新实例???
    }



第二个类文件

    public static void main (String[] args)
    {

        HaaKaMethodsPractice testMethods = new HaaKaMethodsPractice(); // 创建一个MethodsPractice类的新实例。
        //^^^我无法让这个语句工作。

        // 测试displayLine方法。
        testMethods.displayLine(); // 显示一行。

        // 测试setBulb方法
        boolean state = true; // 发送给这个方法进行测试的布尔值的初始值。
        testMethods.setBulb(state); // 调用setBulb方法并传递一个true布尔值。
        state = false; // 将布尔值'state'更改为false,并再次测试。
        testMethods.setBulb(state); // 调用setBulb方法。

        testMethods.displayLine(); // 显示一行。

        // 测试displayMessage方法。
        String message = "这本书有库存。"; // 为要发送到方法的消息设置一个值。
        testMethods.displayMessage(message); // 调用displayMessage方法并发送消息。

        testMethods.displayLine(); // 再显示一行。

        // 测试sumNumbers方法。
        int num1 = 7; // 给num1一个初始值。
        int num2 = 5; // 给num2一个初始值。
        int totalOfNumbers = 0; // 将初始值设置为0。
        totalOfNumbers = testMethods.sumNumbers(num1, num2); // 设置一个变量来保存返回的值,并发送数字。
        System.out.println("两个数字的总和是:" + totalOfNumbers); // 显示结果以查看是否正确。

        testMethods.displayLine(); // 再显示一行。

        // 测试isGreater方法。使用前一个方法的num1和num2。
        boolean greater = false; // 为布尔值greater赋予初始值。
        greater = testMethods.isGreater(num1, num2); // 设置一个变量来保存返回的值,并发送数字。
        System.out.println("第一个数字更大吗?" + greater); // 显示结果以查看是否正确。

        testMethods.displayLine(); // 再显示一行。
        testMethods.displayLine();
     }
英文:

I have a college assignment to practice with testing methods in Java, I can't figure out why I'm getting the error, "This class does not have a static void main method accepting String[]." I'm using Dr. Java and another thing to note is I'm very new to programming so the solution may be rather obvious.

Main class file.

public static void displayLine() //Displays line
{
System.out.println("--------------------"); //print line
}
public static void displayMessage(String message) //show message 
{
System.out.println(message); //display message
}
public static int sumNumbers(int num1, int num2) //method for returning the sum
{
return (num1 + num2); //add the two numbers
}
public static boolean isGreater(int num1, int num2) //see if the number is greater.
{
boolean isGreater; 
if (num1 > num2) //conditional statement to see which number is larger
{
isGreater = true;
} else {
isGreater = false;
}
return isGreater; //returns whether or not it is greater
}
public static void setBulb(boolean state) //determines if bulb is on or off
{
if (state == false) //conditional statemtnt to show whether the bulb is on or off.
{
System.out.println("The bulb is now off");
} 
else 
{
System.out.println("The bulb is now on");
}
//^^^ I could not get this to work. I wonder if it is because I didn't create a new instance of the method???
}

Second class file.

public static void main (String[] args)
{
HaaKaMethodsPractice testMethods = new HaaKaMethodsPractice(); // Create a new instance of the MethodsPractice class.
//^^^I could not get this statement to work.
// Test the displayLine method.
testMethods.displayLine(); // Display a line.
// Test for the setBulb method
boolean state = true; // Initial value for the boolean sent to test this method.
testMethods.setBulb(state); // Invoke the setBulb method and pass it a boolean of true.
state = false; // Change the boolean 'state' to false, and test again.
testMethods.setBulb(state); // Invoke the setBulb method.
testMethods.displayLine(); // Display a line.
// Test for the displayMessage method.
String message = "This book is in stock."; // Set a value to be sent to the method.
testMethods.displayMessage(message); // Invoke the displayMessage method and send the message.
testMethods.displayLine(); // Display another line.
// Test for the sumNumbers method.
int num1 = 7; // Give num1 an initial value.
int num2 = 5; // Give num2 an initial value.
int totalOfNumbers = 0; // Set the initial value to 0.
totalOfNumbers = testMethods.sumNumbers(num1, num2); // Set a variable to hold the returned value and send the numbers.
System.out.println("The total of the two numbers is: " + totalOfNumbers); // Display the result to see if it is correct.
testMethods.displayLine(); // Display another line.
// Test for the isGreater method. Uses num1 and num2 from previous method.
boolean greater = false; // Give an initial value to boolean greater.
greater = testMethods.isGreater(num1, num2); // Set a variable to hold the returned value and send the numbers.
System.out.println("First number greater? " + greater); // Display the results to see if they are correct.
testMethods.displayLine(); // Display another line.
testMethods.displayLine();
}

答案1

得分: 1

你不会创建'方法的实例'。你创建类的实例。而你在下面添加评论的那个方法是一个静态方法。静态有时可能会让人感到困惑,并且往往会让你陷入一种与Java最佳实践不符的代码思维模式中,所以,停止使用静态。我建议你的main方法只包含一行代码:new MyThingie().go(args);,如果你不打算使用命令行参数,可以省略args,而go才是真正的主要方法(而且不是静态的)。这是因为main必须是静态的,但在你的Java职业生涯前几周,除此之外,你的代码库中不应该有其他静态内容。

至于错误:你正在运行一个类或源文件。你正在运行的那个文件?那个文件需要有public static void main(String[] args)方法。

例如,将setBulb设置为静态是相当不合逻辑的。这会假设整个地球上只有一个灯泡,甚至'两个灯泡'的概念都是无意义的(想象一下'两个存在感'的概念 - 有些事物不存在数量,它们只是存在。这就是静态方法的特点。添加数字的概念?那不受数量的概念限制。但灯泡是有数量概念的,所以setBulb绝对不应该是静态的,这只是一个例子)。

英文:

You don't create 'instances of a method'. You create instances of a class. And that method you put that comment under is a static method. static can be confusing at times, and tends to lead you straight into a model of thinking about your code that clashes with how java works best, so, stop using static. I suggest your main method contains one single line of code: new MyThingie().go(args);, where you can leave out the args if you aren't going to use the command line args, and go is the actual real 'main' method (and is not static). That's because main has to be static, but that should be the only static thing in your code base until you're many weeks into your java career.

As to the error: You are running a class or source file. The one you're running? That one needs to have that public static void main(String[] args) method.

For example, setBulb being static is rather illogical. That would presume there is only one bulb on the entire planet and even the concept of 'two bulbs' is non-sensical (imagine the concept of 2 'sense of existence' - some things do not come in amounts, they just are. That's what static methods are like. The concept of adding numbers? That's not subject to the concept of amounts. But bulbs are, so setBulb definitely should not be static, just an example).

huangapple
  • 本文由 发表于 2020年9月30日 23:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64141306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定