2013-10-05 92 views
0

嗨,大家好,我是一名新的計算機科學專業的學生,​​我試圖調用一種方法,並且一直在收到此錯誤。這裏是我的問題區域:錯誤:無效的方法聲明;返回類型ERROR在調用方法時

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

} 
}  

回答

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... 
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); 

} 
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類型;他們不需要。

相關問題