0

我不知道为什么我的第二个 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 < 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;
    }

我已经清理了一下,现在出现的情况与输入相同

4

3 回答 3

1

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

我认为,您需要更改while如下(移出right--条件if):

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

还有两个问题:

请在 while 循环之前重新初始化 occurence =0;,以便计算每个数字的出现次数并4System.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");
        }
于 2012-10-25T21:39:58.280 回答
0

快速浏览一下,并对未来提出建议。

这段代码:

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

建议如果 list[left] != list[right],但 right 仍然 >= left,则此循环将永远不会停止。如果您想要的只是计算找到的次数,您可能想要移动 if 语句的右侧 - OUTSIDE。

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

于 2012-10-25T21:37:34.280 回答
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");
    }
于 2012-10-25T21:37:45.977 回答