2012-01-12 1 views
1

私はボトル回収プログラムを作っています。私は4つの部屋からボトルを収集して入力しています。ユーザーが終了すると、その部屋が収集したボトルのリストが表示され、勝者が選出されます。私のコードは現在最高記録ボトル数を選んだが、そのボトル数を持つ部屋番号を表示したい。コードを変更して動作させるにはどうすればよいですか?最高のレコード番号を持つ配列番号を選択するにはどうすればよいですか?

namespace BottleDrive 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { //Initialize loop rooms to 4 
      int[] rooms = new int[4]; 
      //Start of while loop to ask what room your adding into. 
      while (true) 
      { 
       Console.Write("Enter the room you're in: "); 
       //If user enters quit at anytime, the code will jump out of while statement and enter for loop below 
       string quit = Console.ReadLine(); 
       if (quit == "quit") 
        //Break statement separates embedded statement and is needed inorder to allow 
        break; 


       //Variable room holds the number of bottles collect by each room. 
       int room = int.Parse(quit); 
       Console.Write("Bottles collected in room {0}: ", room); 
       // This line adds the count of bottles and records it so you can continuously count the bottles collected. 
       rooms[room - 1] += int.Parse(Console.ReadLine()); 
      } 
      //This for statement lists the 4 rooms and their bottle count when the user has entered quit. 
      for (int i = 0; i < rooms.Length; ++i) 
       Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]); 

      Console.WriteLine("And the Winner is room " + rooms.Max().ToString() + "!!!"); 
     } 
    } 
} 
+1

ジョージを、あなたがこのプログラムに関するオープン9つの質問、答えとすべて(のhttpを持っていますstackoverflow.com/users/1144168/george-li?tab=questions)。回答があれば、その横にあるチェックマークをクリックして回答を「受け入れる」べきです。 –

答えて

2

これを試してみてください:

 int maxValue = 0; 
     int maxRoomNumber = 0; 
     for (int i = 0; i < rooms.Length; ++i) 
     { 
      if (rooms[i] > maxValue) 
      { 
       maxValue = rooms[i]; 
       maxRoomNumber = i + 1; 
      } 
      Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]); 
     } 

     Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!"); 
+0

デバッグ時にローカル変数maxValueとmaxRoomNumberの割り当てが解除されます。何故ですか? – gli

+0

申し訳ありませんが、あまりにもVB:私は答えを更新しました。 –

1

rooms.Max()のご利用には、それが最大値を返すだろうということを除いて近いですし、インデックスをしたいです。

単純なforループを使用してインデックスを見つけることができます。それ以外の場合はcould go with an elegant "LINQ" methodです。

注:複数の部屋の最大値が同じであることを忘れないでください。

1

LINQにIndexOf()拡張が含まれていないのはなぜかと思いました。それと

public static class Extensions 
{ 
    public static int IndexOf<T>(this IEnumerable<T> items, Predicate<T> predicate) 
    { 
     int index = 0; 
     foreach (T item in items) 
     { 
      if (predicate(item)) 
       break; 
      index++; 
     } 

     return index; 
    } 

    public static int IndexOf<T>(this IEnumerable<T> items, T value) 
    { 
     int index = 0; 
     foreach (T item in items) 
     { 
      if (item.Equals(value)) 
       break; 
      index++; 
     } 

     return index; 
    } 
} 

あなたは、単に行うことができます:私は、私がプロジェクトに含まれていることを私自身を書いてしまった//:

Console.WriteLine("And the Winner is room " + rooms.IndexOf(rooms.Max())); 
関連する問題