1、以下代码的结果是什么?
 begin
  当前数:=1;
  While 当前数<8 then
    当前数:=当前数+2;
    结果:=2*当前数+3;
  End loop;
  rentun 结果;
end;2、以下代码执行循环的次数是多少?
LoopCounts
begin
  结果:=0;
  当前数:=1;
  For 当前数 in (1...10) loop
    结果:=结果+当前数;
    If 结果>17 then
      退出循环;
    End if;
  end loop;
end;

解决方案 »

  1.   

    //This is a test file
    public class Test
    {
    public int get()
    {
    int i = 1;
    int result = 0;
    while (i < 8)
    {
    i += 2;
    result = 2 * i + 3;
    }
    return result;
    }
    public static void main(String[] args)
    {
    Test test = new Test();
    int result = test.get();
    System.out.println(result);

    }
    }第一个:输出结果是:21
      

  2.   

    晕,因为你的result没有+=啊,只是=而已每次都重新赋值,结果当然就是最后一次循环的结果啦
      

  3.   

    public class Test{

    public int get(){

    int result = 0;
    for (int i = 0; i <= 10; i++){

    result += i;
    if (result > 17){

    break;
    }
    }
    return result;
    }

    public static void main(String[] args){

    Test test = new Test();
    int result = test.get();
    System.out.print(result);

    }
    }
    第二个:结果是21 循环次数:6
      

  4.   

    //This is a test file
    public class Test
    {
        public int get()
        {
            int i = 1;
            int result = 0;
            do
            {
                i += 2;
                result = 2 * i + 3;
            }
            while (i < 8);
            return result;
        }
        public static void main(String[] args)
        {
            Test test = new Test();
            int result = test.get();
            System.out.println(result);
            
        }
    }
    那你看下这个程序呢?
      

  5.   

    这2个循环方法在这个例子中是一样的效果
    区别是
    直接while循环:第一次循环也需要判断是否为真
    do while:第一次循环无视条件,从第2次开始需要判断条件是否为真当i=7的时候,
    i+=2;
    这时,i已经等于9了,然后i*2+3,就等于9*2+3=21