【问题标题】:Return statements in java with integers用整数返回java中的语句
【发布时间】:2015-08-08 19:23:10
【问题描述】:

在处理整数的 java 中,我无法掌握四种不同的返回语句问题。它们通常以“神秘”和“谜”的名义出现。我已经尝试过无数次解决它们,而网络和我的教科书不包含任何类似的示例供我使用。我的直觉是我在逻辑上遗漏了一些东西。如果有人可以解释这些问题(最好是最难的问题),我相信我会理解的。如果格式不符合本站要求,我提前致歉,因为这是我第一次发帖。

 1   public class Program 1{
 2
 3   public static int y = 2;
 4
 5   public static int mystery(int x, int y) {
 6      y = y + x;
 7      return x + y;
 8   }
 9
10   public static void main(String[] args) {
11      int x = 1;
12      x = mystery(x, y);
13      y = mystery(y, x);
14      System.out.println(x + " " + y);
15   }
16}
17 // Answer : 4 8
 I get x to be 4, but I struggle to get y to be 8.


1 public class Program 2{
 2
 3   public static int y = 2;
 4
 5   public static int mystery(int a, int b) {
 6      y = b + a;
 7      return a + b;
 8   }
 9
10   public static void main(String[] args) {
11      int x = 1;
12      x = mystery(x, y);
13      y = mystery(y, x);
14      System.out.println(x + " " + y);
15   }
16}
17 // Answer : 3 6
 I get x to be 3, but I struggle to get y to be 6.


1 public class Program 3{
 2
 3   public static int x = 1;
 4   public static int y = 2;
 5
 6   public static int mystery1(int a, int b) {
 7      x = a + b;
 8      return b + a;
 9   }
10
11   public static int mystery2(int a, int b) {
12      y = b + a;
13      x = mystery1(a, b);
14      return a + b;
15   }
16
17   public static void main(String[] args) {
18      x = mystery2(x, y);
19      System.out.println(x + " " + y);
20   }
21
22}
23 // Answer : 3 3
 I get x to be 3, but I struggle to get y to be 3.



public class Enigma {

public static int x = 1;
public static int y = 2;
public static int n = 0;

public static int aaa(int a, int b) {
n++; 
return a + b;
}

public static int bbb(int a, int b) {
n++; x = aaa(x, a); y = aaa(y, b); 
return x + y;
}

public static void ccc(int x, int q) {
n++; x = bbb(1, x); y = bbb(2, q);
}

public static void main(String[] args) {
int x = aaa(3, y); 
y = bbb(x, y); ccc(x, 1);
System.out.println(x + " " + y + " " + n);
// Answer : 5 25 11
I get x to be 5, but I struggle to get y = 25 and n = 11, although incrementing n at each method is probably the reason why.

【问题讨论】:

  • 您在程序 1 的 mystery 方法中使用一个也称为 y 的参数来隐藏您的类变量 y
  • 类名应该是一个单词(目前不是两个)
  • 是的,我知道,为了便于阅读,我在发帖前将其隔开。

标签: java methods static integer return


【解决方案1】:

程序1:

x =神秘(x,y)->返回4,所以x=4,你的类y保持2(如果你想访问y类,使用'this'关键字。

y =神秘(y, x) -> 神秘(2,4) 返回2+6=8

程序2:

x =神秘(x, y) -> y 类现在是 3,返回 3,x=3

y =神秘(y,x)->神秘(3,3)返回6

程序3:

x =神秘2(x, y) -> y 类现在是 3,神秘1(1, 2) -> 返回 3,所以类 x=3

谜团:

int x = aaa(3, y) -> 返回 5, n=1

y = bbb(x, y) -> n=4, x=6, y=4, 返回 10. 所以类 y=10

在方法 ccc 中,n 变为 11 (1+3+3)。 请注意,在方法 ccc 中,您正在隐藏类成员 x。相反,它是参数中的本地参数。

【讨论】:

    【解决方案2】:

    不要一直使用相同的变量,尽量减少混乱。例如,在主程序中,创建新变量(比如说 a 和 b),而不是写“int a=mystery(x,y);”和“int b=mystery(y,x);”对于程序 1。

    1) 让我们先解决程序 1 中的问题。首先程序运行第 12 行代码,即 x=mystery(x,y),x 为 1,y=2。
    2) 所以 x=mystery(1,2)。当你运行它时,你分配 y=y+x,y 仍然是 2,x 仍然是 1,2+1=3,因此现在 y 是 3,x 仍然是 1。 3) 然后你告诉程序返回 y+x 或 3+1,然后你把它赋值给 x。现在 x=4 并且 y 是 3。 4) 你的程序运行第 13 行,y=3 和 x=4,因此 y=(3,4)。所以 y=y+x 是 y=3+4,或 7。 5)这是你搞砸的时候!你期望 x 仍然是 1,但现在不是了,它是 4,因此你得到 11。

    如果您不明白,请发表评论。如果可能的话,请使用我提供的编号告诉我你在哪里卡住了:) 祝你好运

    【讨论】:

    • 我明白你的意思了!谢谢!
    猜你喜欢
    • 2011-01-03
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2015-05-03
    相关资源
    最近更新 更多