错误:方法声明无效;返回类型必需ERROR调用方法时

时间:2013-10-05 21:40:04

标签: java

嗨,大家好,我是一名新的计算机科学专业的学生,​​我正在尝试调用一种方法,并且我一直在收到此错误。这是我的问题所在:

calcTicket(String driverName, int speedDriving, int speedLimit, String schoolZone);

这是整个代码(它不会太长):

import java.util.Scanner;   
public class SpeedingTicket
{

public static void main(String[] args) 
    //Declarations and Instantiations
{
    // Input Data
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter driver's name: ");
    String driverName = scan.nextLine();

    System.out.println("Enter driver's speed: ");
    int speedDriving = scan.nextInt();

    System.out.println("Enter speed limit: ");
    int speedLimit = scan.nextInt();
    scan.nextLine();

    System.out.println("Was driver in school zone? ");
    String schoolZone = scan.nextLine();

}

    // Call calcTicket

    calcTicket(String driverName, int speedDriving, int speedLimit, String schoolZone);

/** Calculates the cost of a speeding ticket given the speed driven, limit, and if in a school zone.

 */ 
public static void calcTicket(String driverName, int speedDriving, int speedLimit, String schoolZone)
{
    // Define constants here

    final int INIT_ADDITIVE = 50;
    final int NO_MULTIPLIER = 6;
    final int YES_MULTIPLIER = 12; 
    final int MAX_ADDITIVE = 200;
    final int EX_THRESH = 30; 

    // Calculations

    int amountOver = (speedDriving - speedLimit);

    if (schoolZone.equalsIgnoreCase("yes"))
    {
        if (amountOver > 0)
            { int fine = amountOver * YES_MULTIPLIER + INIT_ADDITIVE;
            System.out.println(driverName +"owes: $" + fine);
            }
        if (amountOver > EX_THRESH)
            { int fine = amountOver * YES_MULTIPLIER + MAX_ADDITIVE;
            System.out.println(driverName +"owes: $" + fine);
            }
        else 
            { int fine = 0;
            System.out.println(driverName +"owes: $" + fine);
            }
    }

    if (schoolZone.equalsIgnoreCase("no"))  
    {

        if (amountOver > 0)
            { int fine = amountOver * NO_MULTIPLIER + INIT_ADDITIVE;
            System.out.println(driverName +"owes: $" + fine);
            }
        if (amountOver > EX_THRESH)
            { int fine = amountOver * NO_MULTIPLIER + MAX_ADDITIVE;
            System.out.println(driverName +"owes: $" + fine);
            }
        else 
            { int fine = 0;
            System.out.println(driverName +"owes: $" + fine);
            }
    }

}
}       

3 个答案:

答案 0 :(得分:0)

这部分是问题

// Call calcTicket

calcTicket(String driverName, int speedDriving, int speedLimit, String schoolZone);

你不能让代码只是在这样的类中闲逛。把它放进一个方法 - 我想你想把它放在main()中:

public static void main(String[] args) 
    //Declarations and Instantiations
{
    // Input Data
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter driver's name: ");
    String driverName = scan.nextLine();

    System.out.println("Enter driver's speed: ");
    int speedDriving = scan.nextInt();

    System.out.println("Enter speed limit: ");
    int speedLimit = scan.nextInt();
    scan.nextLine();

    System.out.println("Was driver in school zone? ");
    String schoolZone = scan.nextLine();

//} --this is not good here!

    // Call calcTicket

    calcTicket(String driverName, int speedDriving, int speedLimit, String schoolZone);

   } //put here instead...

答案 1 :(得分:0)

您没有在方法(或构造函数)中调用该方法。将回调放在main方法中。

public static void main(String[] args) 
    //Declarations and Instantiations
{
    // Input Data
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter driver's name: ");
    String driverName = scan.nextLine();

    System.out.println("Enter driver's speed: ");
    int speedDriving = scan.nextInt();

    System.out.println("Enter speed limit: ");
    int speedLimit = scan.nextInt();
    scan.nextLine();

    System.out.println("Was driver in school zone? ");
    String schoolZone = scan.nextLine();

 // Call calcTicket

    calcTicket(String driverName, int speedDriving, int speedLimit, String schoolZone);

}

答案 2 :(得分:0)

当您将代码剪切并粘贴到问题中时,我不确定格式是否会被扭曲,但显然您需要将calcTicket的调用拉入main的正文中。像这样的东西:

...
System.out.println("Was driver in school zone? ");
String schoolZone = scan.nextLine();

}

// Call calcTicket

calcTicket(String driverName, int speedDriving, int speedLimit, String schoolZone);

这样的事情:

System.out.println("Was driver in school zone? ");
String schoolZone = scan.nextLine();

// Call calcTicket
calcTicket(driverName, speedDriving, speedLimit, schoolZone);
}

并确保从通话中删除arg类型;他们不需要。