使用整数返回java中的语句

时间:2015-05-26 22:32:51

标签: java methods static integer return

我在处理整数的java中遇到四个不同的return语句问题时遇到了麻烦。他们通常会以" mystery"的名字命名。和" enigma"。我曾多次尝试解决它们,而网页和我的教科书中没有任何类似的例子可供我使用。我的直觉是我错过了逻辑的东西。如果有人可以解释问题(最好是最困难的问题),我相信我会理解它。如果格式不符合本网站的要求,我会提前告知,因为这是我第一次发帖。

 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.

2 个答案:

答案 0 :(得分:0)

不要继续使用相同的变量,尽量减少混乱。例如,在主程序中,创建新变量(比如说a和b),然后编写" int a = mystery(x,y);"和" int b =神秘(y,x);"对于计划1。

1)让我们首先解决你在程序1中遇到的问题。首先程序运行代码行12,即x = mystery(x,y),x是1,y = 2。 2)所以x =神秘(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。

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

答案 1 :(得分:0)

PROGRAM1:

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

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

Program2中:

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

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

节目3:

x = mystery2(x,y) - > y类现在是3,mystery1(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。相反,它是参数中的本地值。