Java 了解while循环和System.out.println的执行结果

Java 了解while循环和System.out.println的执行结果,java,Java,您好,我目前很难理解添加了数组的while循环的执行过程。我理解数组和循环,只是理解这行代码有困难。我已经搜索了我能想到的每一个来源,但似乎没有什么能帮助我更好地理解它。我在Eclipse中运行了代码,答案显示在我的控制台中。我只是不知道他们是如何处理的。如果有人能帮我,我将非常感激,谢谢 import javax.swing.JApplet; public class SystemExecutions extends JApplet { int x

您好,我目前很难理解添加了数组的while循环的执行过程。我理解数组和循环,只是理解这行代码有困难。我已经搜索了我能想到的每一个来源,但似乎没有什么能帮助我更好地理解它。我在Eclipse中运行了代码,答案显示在我的控制台中。我只是不知道他们是如何处理的。如果有人能帮我,我将非常感激,谢谢

      import javax.swing.JApplet;


     public class SystemExecutions extends JApplet
     {
     int x=1, y=-5, z=4; // global variables 
     int vals[] = {-6,2,-4,-8 ,-2,-3}; // global variables 

      public void init()
      {
     setView();///execution results 
     setValues();///Exact output 
      }

   public void setView()//////////////////////////////execution results 
  { boolean flag = false; 
  int count = 0; int j=0; 
  int array[] = { 5, 3, 5, 5, 3, 5}; 
  System.out.println( ); 
  while ((!flag) && (j < array.length-1)) 
  { if (array[j] == array[j+1] ) 
  flag=true; 
  else count++; 
  ++j; 
  System.out.println("flag " + flag + " J " + j 
  + " count "+ count); 
  } 
  System.out.println("flag " + flag + " J " + j 
  + " count "+ count); 
  } 
         /*Ouput:
            flag false J 1 count 1
            flag false J 2 count 2
            flag true J 3 count 2
            flag true J 3 count 2
         */


   public void setValues()////////////////////////////Exact output 
     {char y = 'R'; 
    z=10; 
    System.out.println("l1: "+x+" "+y+" "+z); 
    y=call1(x,y,z); 
    System.out.println("l2: "+x+" "+y+" "+z); 
    x=call2(x,vals); 
    System.out.println("l3: "+x+" "+y+" "+z); 
    for (int i=0; i<3; i++) 
        System.out.println("l"+(i+4)+": "+vals[i*2]); 
      } 
      public char call1(int a, char b, int c) 
      {if (a >= c) return b; 
        else 
     { c=15; 
     z=25; 
     return 'M'; // note the single quotes 
      } 
     } 
     public int call2(int x, int [] anArray) 
     {int y = 0; 
    for (int i=anArray.length-1; i>=0; i--) 
    {if (anArray[i] > x) 
    {anArray[i] = x + 5;y++;} 
     } 
        x=100; 
     return y; }

     }
        /*Output:
            l1: 1 R 10
            l2: 1 M 25
            l3: 1 M 25
            l4: -6
            l5: -4
            l6: -2
          */
import javax.swing.JApplet;
公共类SystemPlet
{
int x=1,y=-5,z=4;//全局变量
int vals[]={-6,2,-4,-8,-2,-3};//全局变量
公共void init()
{
setView();///执行结果
setValues();///精确输出
}
public void setView()//执行结果
{布尔标志=false;
int count=0;int j=0;
int数组[]={5,3,5,5,3,5};
System.out.println();
而((!flag)&(j=0;i--)
{if(anArray[i]>x)
{anArray[i]=x+5;y++;}
} 
x=100;
返回y;}
}
/*输出:
l1:1 R 10
l2:1米25
l3:1米25
l4:-6
l5:-4
l6:-2
*/
当条件满足时(由于阵列中两个5相邻)


如果将您的标志置为true,变量j将再次递增,因此j和count之间存在差异。

if
语句和
else
语句中没有主体,则
{
}
意味着在条件
if
else
下只执行下一个命令

因此,在您的情况下,在循环内部:

if (array[j] == array[j+1] ) 
  flag=true; 
  else count++; 
  ++j; 
如果
if
为真,则标志设置为真,如果不为真,则计算
count
。 语句
j++
在循环的每次迭代中执行,因为它不受
if
else
对的影响


这可能是这里的混淆。

在要检查的代码之前放置一个断点,然后运行调试器并一步一步地检查它。或者使用铅笔和纸;-)谢谢你的回复。我的教授向我们班提到了使用铅笔和纸。谢谢你们的回答。这真的帮助很大。谢谢你们的回答。这个部分有点难理解,尤其是当国旗被悬挂的时候!标志,布尔值为false。
if (array[j] == array[j+1] ) 
  flag=true; 
  else count++; 
  ++j;