【问题标题】:To know the logic behind it了解其背后的逻辑
【发布时间】:2011-05-31 13:35:42
【问题描述】:

有没有人给我解释一下这个编码

public class FunctionCall {

  public static void funct1 () {
    System.out.println ("Inside funct1");
  }

  public static void main (String[] args) {
    int val;
    System.out.println ("Inside main");
    funct1();
    System.out.println ("About to call funct2");
    val = funct2(8);
    System.out.println ("funct2 returned a value of " + val);
    System.out.println ("About to call funct2 again");
    val = funct2(-3);
    System.out.println ("funct2 returned a value of " + val);
  }

  public static int funct2 (int param) {
    System.out.println ("Inside funct2 with param " + param);
    return param * 2;
  }
}

【问题讨论】:

  • 你需要解释什么?

标签: java


【解决方案1】:

当然可以,但问题出在哪里?

有2个静态方法

public static void funct1 () {
  System.out.println ("Inside funct1"); 
}

public static int funct2 (int param) {
  System.out.println ("Inside funct2 with param " + param); 
  return param * 2; 
}

控制台上的输出将是(如果我没有计算错误):

Inside main
Inside funct1
About to call funct2
Inside funct2 with param 8
funct2 returned a value of 16
About to call funct2 again
Inside funct2 with param -3
funct2 returned a value of -6

从main开始,然后进入funct1一次;然后它用 8 调用 func2,然后用 -3 调用另一个时间

【讨论】:

  • 先生你能告诉我这个程序的输出是什么吗1:class Blanks { 2: public static void main(String[] arguments) { 3: System.out.println("The " + arguments [0] 4: + " " + arguments[1] + " fox " 5: + "跳过了 " 6: + arguments[2] + "dog."); 7: } 8: }
  • 这样读起来很难,但是这个程序似乎需要 3 个参数,并根据它打印出一个句子
【解决方案2】:
public class FunctionCall {//a public class

  public static void funct1 () { /a static method which returns nothing void
    System.out.println ("Inside funct1");
  }

  public static void main (String[] args) {//main method
    int val;//int variable declaration
    System.out.println ("Inside main");//printing on console
    funct1();//calling static method
    System.out.println ("About to call funct2");////printing on console
    val = funct2(8);//calling static method which returns int and assigning it to val variable
    System.out.println ("funct2 returned a value of " + val);//printing on console
    System.out.println ("About to call funct2 again");//printing on console
    val = funct2(-3);////calling static method which returns int and assigning it to val 
    System.out.println ("funct2 returned a value of " + val);//printing on console
  }

  public static int funct2 (int param) {
    System.out.println ("Inside funct2 with param " + param);
    return param * 2;
  }
}

【讨论】:

  • 你能推荐我最好的安卓书吗(待学)
  • 您发布的问题与android无关,它是简单的核心java,如果您需要有关android的帮助,请继续提出新问题
猜你喜欢
  • 2017-05-07
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
  • 1970-01-01
相关资源
最近更新 更多