Java变量跟踪表单

时间:2016-03-24 17:14:44

标签: java variables tracing

对于作业,我应该完成一个表格,跟踪myNum和mySum的值,以及基于以下代码段的任何输出:

int myNum = 11;
int mySum = 0;

while (myNum >= 0) 
{
myNum--;
mySum += myNum * 5;
}

System.out.println ("myNum: " + myNum);
System.out.println ("mySum: " + mySum);

我对Java很新,我在理解这个细分市场会产生什么方面遇到了一些麻烦。任何人都可以帮忙澄清一下吗?

2 个答案:

答案 0 :(得分:0)

这是评论版:

int myNum = 11; // myNum is 11
int mySum = 0; // mySum is 0

while (myNum >= 0) // repeat the next block (between {}) while myNum is positive or 0
{
    myNum--; // decrease myNum by 1
    mySum += myNum * 5; // add 5 times myNum to mySum
}

System.out.println ("myNum: " + myNum); // output "myNum: " followed by myNum value
System.out.println ("mySum: " + mySum); // same as above by with mySum

答案 1 :(得分:0)

myNum的值为:11,10,9,8,7,6,5,4,3,2,1,0,-1mySum的值为:0,50,95,135,170,200,225,245,260,270,275,275,270。所以, 输出将是:

    myNum: -1
    mySum: 270