我对java很新,我在准备考试时遇到了麻烦。此示例问题要求显示工作以提供此代码生成的确切输出:
public class Lab7Experiment extends JApplet
{
int x=3, y=-3, z=5;
int someValues [] = {2, -6, 4, -4, 6, -2};
public void init()
{
char y = 'y';
z=10;
System.out.println("Print 1 x,y,z, = "+x+" "+y+" "+z);
y= sub1(x,y,z);
System.out.println("Print 2 x,y,z, = "+x+" "+y+" "+z);
x= sub2(x, someValues);
System.out.println("Print 3 x,y,z, = "+x+" "+y+" "+z);
for (int i=0; i<3; i++)
System.out.println("Print "+(i+4)+" "+someValues[i*2]+" "+someValues[i*2+1]);
}
public char sub1(int a, char b, int c)
{
if (a>=c)
return b;
else
{
c=15;
z=25;
return 'z';
}
}
public int sub2(int x, int [] anArray)
{
int y=0;
for(int i=anArray.length-1; i>=0; i--)
{
if (anArray[i] > x)
{
anArray[i]=x;
y++;
}
}
x=100;
return y;
}
}
我无法理解在生成此输出时如何使用someValues数组和anArray。我已经在Eclipse中运行它,所以我能够检查我的答案,但我不确定为什么生成的输出结果就是它们。谁可以给我解释一下这个?非常感激。感谢
答案 0 :(得分:0)
这是一个很长的答案,而不是要解决的问题......
顺便说一句:
因为类变量x
= 3:int x=3
那么局部变量char y =&#39; y&#39;已使用,z
已更改为10 z=10;
打印2 x,y,z,= 3 z 25
因为类变量x
仍未触及。在sub1
中:3不是&gt; = 10(传递z
),所以else
分支:
`c=15;` does not change passed `x` Java is `pass-by-value`
`z=25;` changes class variable `z` to 25
return 'z';
然后本地变量y现在是&#39; z&#39;
你有:
class variable x = 2
class variable y =-3
class variable z = 25
local variable y = 'y'
更新:class variable someValues [] = {2, -6, 3, -4, 3, -2};
然后按照上一个循环从someValues
获取Print 4,5和6作为:
`Print 4 2 -6`
`Print 5 3 -4`
`Print 6 3 -2`
祝你好运!