将数字,变量和过程作为Java中的参数传递

时间:2013-08-02 01:37:40

标签: java argument-passing

我刚开始研究如何用Java编程语言编写代码。

我遇到了一个问题,告诉我将数字,变量和表达式作为过程调用的参数传递。我遇到的问题是,当我尝试将数字,变量和表达式作为参数传递给过程调用时,我收到错误(我有27个错误)。

以下是我的代码,如果有人能指出我的代码有什么问题,我将非常感激。谢谢。

public class test {

// this is the procedure definition
public static int computeCost ( int quantity , int price ) {
    return quantity * price;
}

public static void main ( String args[] ) 
    // passing numbers as arguments to the procedure call "cost"
    System.out.println ( computeCost ( 7 , 12 ) );

    // passing variables as arguments to the procedure call "cost"
    int a = 5;
    int b = 7;
    System.out.println ( computeCost ( a , b ) );

    // passing expressions as arguments to the procedure call "cost
    System.out.println ( computeCost ( 1 + 2 + 3 + 4, 5 + 6 + 7 + 8 ) );
}
}

4 个答案:

答案 0 :(得分:5)

我明白了什么是错的。在main(..)方法之后没有左括号。 Java中的所有方法都必须使用{}包围其代码。

改变这个:

public static void main ( String args[] )

到此:

public static void main ( String args[] ) {

除此之外,你的代码对我来说非常好。

答案 1 :(得分:3)

您在主方法上缺少一个左括号。

public class Test
{
    // this is the procedure definition
    public static int computeCost(int quantity, int price)
    {
        return quantity * price;
    }

    public static void main(String args[])
    {// <--MISSING
        // passing numbers as arguments to the procedure call "cost"
        System.out.println(computeCost(7, 12));

        // passing variables as arguments to the procedure call "cost"
        int a = 5;
        int b = 7;
        System.out.println(computeCost(a, b));

        // passing expressions as arguments to the procedure call "cost
        System.out.println(computeCost(1 + 2 + 3 + 4, 5 + 6 + 7 + 8));
    }
}

答案 2 :(得分:2)

您错过了{的定义中的开头main

public static void main ( String args[] ) 

应该是

public static void main ( String args[] ) {

答案 3 :(得分:0)

只是小费。有时,太多错误只对应于缺少一段代码。 寻找每一种可能的情况。