0

2番目の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

あなたは2番目forも働いています。問題はwhileループ状態にありますwhile ( right >= left )。が等しくない場合list[left] == list[right]、それは次のように無限ループになり、その場合は変化しrightませんleft

私はあなたが以下のようにあなたを変える必要があると思います(条件の外にwhile移動します):right--if

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

さらに2つの問題:

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");
        }
于 2012-10-25T21:39:58.280 に答える
0

ちょっと見て、そして将来への提案。

このコード:

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

list [left]!= list [right]でありながら、rightがまだ> = leftである場合、このループは決して停止しないことを示唆しています。右に移動することをお勧めします。ifステートメントの外側で、検出されたオカレンスの数だけが必要な場合。

最後に、「ループが機能しない」と言うとき、それは少し役に立たないです。おそらくあなたの出力、またはそれが機能しない方法を表示する方が良いでしょう。

于 2012-10-25T21:37:34.280 に答える
0

どのようなエラーが発生しますか?

私が目にする差し迫った問題の1つは、発生がない場合、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");
         }
     }

そして2番目のループの場合:

    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 に答える