当前位置: 代码迷 >> Java相关 >> 怎样使用循环
  详细解决方案

怎样使用循环

热度:378   发布时间:2011-03-26 00:02:41.0
怎样使用循环
for (i=0;i<4 ;i++ )
                    {
                        if (positionNumber==4)
                        {
                            for ( m=0;m<4 ;m++ )
                            {
                                System.out.print(a[m]);
                            }
                           
                             break;
                        }
                        else if (positionNumber!=4)
                        {
                            //a[i]==temp;
                            if (i==0)
                            {
                                a[i] = randomNumber.nextInt(9)+1;
                                
                                    for (k=0,j=i;k<=m ;k++ )
                                    {
                                        if (a[i]!=b[j][k])
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            for (int n=0; ;n++ )
                                            {
                                                a[i] = randomNumber.nextInt(9)+1;
                                                if (a[i]!=b[j][k])
                                                {
                                                    break;
                                                }
                                            }
                                            
                                            k=-1;
                                        }
                                    }
                                
                              
                            }
                            else if(i!=0)
                            {
                                a[i] = randomNumber.nextInt(10);
                                
                                    for (k=0,j=i;k<=m ;k++ )
                                    {
                                        if (a[i]!=b[j][k])
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            for (int n=0; ;n++ )
                                            {
                                                a[i] = randomNumber.nextInt(10)+1;
                                                if (a[i]!=b[j][k])
                                                {
                                                    break;
                                                }
                                            }
                                            k=-1;
                                        }
                                    }
                                    
                                
                            }
                            for (k=0;k<a.length ;k++ )
                            {
                                 System.out.print(a[k]);
                            }
                            System.out.print("猜对几个数字:");
                            rightNumber = in.nextInt();
                            System.out.print("猜对几个位置:");
                            positionNumber = in.nextInt();
                            if (positionNumber<tempPosition)
                            {
                                positionNumber=tempPosition;
                                
                            }
                           else if (positionNumber>tempPosition)
                           {
                                tempPosition = positionNumber;
                                
                           }
                           else
                          {
                               for (j=0;j<10 ;j++ )
                               {
                                   m=j;
                                   b[i][j]=a[i];
                                   i=i-1;
                                   break;
                               }
                              
                          }
                        //}
                        }
怎样把这段代码优化
----------------解决方案--------------------------------------------------------
很明显有代码是重复的,写成方法调用
----------------解决方案--------------------------------------------------------
  相关解决方案