Java Can';我想不出是什么';是什么导致了这个错误;此类没有接受字符串[]的静态void main方法;

Java Can';我想不出是什么';是什么导致了这个错误;此类没有接受字符串[]的静态void main方法;,java,Java,我有一个大学作业要练习Java中的测试方法,我不明白为什么会出现错误,“这个类没有接受字符串[]的静态void main方法。”我使用的是Dr.Java,另外需要注意的是,我对编程非常陌生,所以解决方案可能相当明显 主类文件 public static void displayLine() //Displays line { System.out.println("--------------------"); //print line } public stati

我有一个大学作业要练习Java中的测试方法,我不明白为什么会出现错误,“这个类没有接受字符串[]的静态void main方法。”我使用的是Dr.Java,另外需要注意的是,我对编程非常陌生,所以解决方案可能相当明显

主类文件

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???
}
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();
 }
第二类文件

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???
}
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();
 }

您不创建“方法实例”。创建类的实例。而你把评论放在下面的方法是一个静态方法。静态有时会让人困惑,而且往往会让您直接进入一种思考代码的模式,这种模式与java的最佳工作方式相冲突,因此,停止使用静态。我建议您的主方法只包含一行代码:
newmythingie().go(args)
,如果不打算使用命令行args,可以省略
args
,并且
go
是实际的“main”方法(不是静态的)。这是因为
main
必须是静态的,但在java职业生涯开始数周之前,这应该是代码库中唯一静态的东西

至于错误:您正在运行一个类或源文件。你跑的那个?这需要有
公共静态void main(String[]args)
方法


例如,
setBulb
是静态的是相当不合逻辑的。这将假定整个地球上只有一个灯泡,甚至“两个灯泡”的概念都是非感官的(想象一下2‘存在感’的概念——有些东西不是以数量出现的,它们只是数量。这就是静态方法的含义。数字相加的概念?这不受数量概念的约束。但是灯泡是,所以setBulb绝对不应该是静态的,只是一个例子).

您试图如何运行代码?您正在执行哪个类?另外,由于您是编程新手,您是否在类中定义了方法?您使用哪一行启动此程序?公共静态void main(String[]args)在哪个类中?