Java基本循环

时间:2012-10-25 21:31:17

标签: java loops

我不知道为什么我的第二个for循环不会执行..它编译但是当我运行它时它不起作用~_~

import java.util.Scanner;

public class ranges
{


 public static void main (String[] args)

  {

 Scanner scan = new Scanner (System.in);

       int size;
       int input;
       int count = 0;
       int occurence = 0;
       System.out.print ("Please enter the size of your array: ");
       size = scan.nextInt();
       int[] list = new int[size];



       for (int index = 0 ; index < list.length ; index++)
       {
           System.out.print ("Please enter an integer between 0 to 50: ");
           input = scan.nextInt();

           if ( input >= 0 && input <= 50 )
           {
               list[index] = input;
            }
           else
           {
               System.out.println ("Invalid output, please try again");
               index--;
           }
        }




       int right = (list.length)-1;
       int left = 0;

        for (int counter = left ; counter < list.length ; counter++)
        {
            while ( right >= left )
            {
                if ( list[left] == list[right] )
                {
                    occurence++;
                    right--;
                }
            }
           right = (list.length)-1;
           System.out.println ("The number " + list[left] + " was added " + occurence + "4 times");
        }

         for (int value : list)
        {
            System.out.print (value + " ");
        }
       ;








    }
}

我更新了for循环来评估出现的情况

  

for(int left = 0; left&lt; list.length; left ++)

     

{

        while ( right >= left )
        {
            if ( list[left] == list[right] )
            {
                occurence++;

            }
            right--;
        }


       System.out.println ("The number " + list[left] + " was added " + occurence + " times");
       right = (list.length)-1;
       occurence = 0;
    }

我已将它清理了一下,现在出现与输入相同

3 个答案:

答案 0 :(得分:1)

你第二个for也在工作。问题出在while循环条件中,即while ( right >= left )。如果list[left] == list[right]不相等,那么它会进入无限循环,因为在这种情况下,rightleft会发生变化。

我认为,您需要更改while,如下所示(在right--条件之外移动if):

  while ( right >= left )
   {
     if ( list[left] == list[right] )
      {
        occurence++;
      }
      right--;
    }

还有两个问题:

请在while循环之前重新初始化 occurence =0;,以便计算每个号码的出现次数并从4中删除System.out.println(),例如下面:

for (int counter = left ; counter < list.length ; counter++)
{ 
  occurence = 0; //< initialize to 0
  while ( right >= left )
  {
      if ( list[left] == list[right] )
      {
         occurence++;
       }
           right--;
   }
   right = (list.length)-1;
       //remove 4 after "occurance +"
   System.out.println ("The number " + list[left] + 
                                            " was added " + occurence + " times");
 }

编辑:使用HashMap工作示例:

       Map<Integer, Integer> scannedNums = new HashMap<Integer, Integer>();
        for (int counter = left ; counter < list.length ; counter++)
        {
            if(scannedNums.get(list[counter]) == null){
                scannedNums.put(list[counter], 1);
            }else{
                int currentCount = scannedNums.get(list[counter]);
                scannedNums.put(list[counter], currentCount+1);
            }
        }

        Set<Integer> nums = scannedNums.keySet();
        Iterator<Integer> numIter = nums.iterator();
        while(numIter.hasNext()){
            int number = numIter.next();
            System.out.println ("The number " + number + 
                    " was added " + scannedNums.get(number) + " times");
        }

答案 1 :(得分:0)

快速浏览一下,建议未来。

此代码:

  while ( right >= left )
    {
        if ( list[left] == list[right] )
        {
            occurence++;
            right--;
        }
    }

建议如果列表[left]!= list [right],但右边仍然是&gt; = left,这个循环永远不会停止。如果你想要的只是计算出现次数的计数,你可能想要移动if语句的右边 - OUTSIDE。

最后,当你说“循环不起作用”时,这有点不太有帮助。也许显示你的输出,或者它不起作用的方式会更好。

答案 2 :(得分:0)

它给出了什么错误?

我看到的一个直接问题是,如果没有出现,while循环将永远运行。第一个for循环也可以使索引超出范围。

对于第一个循环,而不是:

    for (int index = 0 ; index < list.length ; index++)
   {
       System.out.print ("Please enter an integer between 0 to 50: ");
       input = scan.nextInt();

       if ( input >= 0 && input <= 50 )
       {
           list[index] = input;
        }
       else
       {
           System.out.println ("Invalid output, please try again");
           index--;
       }
    }

我愿意:

    for (int index = 0 ; index < list.length ; ++index)
    {
         while(true)
         {
             System.out.print ("Please enter an integer between 0 to 50: ");
             input = scan.nextInt();
             if ( input >= 0 && input <= 50 )
             {
                list[index] = input;
                break;
             }
             System.out.println ("Invalid output, please try again");
         }
     }

对于第二个循环:

    for (int counter = left ; counter < list.length ; counter++)
    {
        occurence = 0;
        for( int i = counter; i <= right; ++i)
        {
            if( list[left] == list[i] )
                 ++occurence;
        }
       System.out.println ("The number " + list[left] + " was added " + occurence + " times");
    }