2012-01-17 6 views
0

私のプログラムは、4つの部屋で収集されたボトルの数を収集します。ユーザーがいつでも終了すると、プログラムはwhile lookから抜け出し、ボトル収集の結果を表示します。その後、ボトル数が最も多い勝利の部屋を計算します。whileループでプロンプトの前にwritelineを書く方法は?

私はなぜ私がそれを入力することはできません理解していないループがあります。 whileループに入るには、私の配列で使用されている1から4までの数字と、部屋のそれぞれが収集したボトルの数を入力するよう促します。私がいつでも入力を中止すると、プログラムはボトルの録音を中止し、最も多くのボトルを持った勝利部屋と結果を吐き出します。

ボトル数を入力する前に、「あなたの部屋番号を入力してください」と表示するにはどうすれば最初に表示されますか?私の問題はgetBottles()にあります

私はこの行を配列で使うことはできないと思いますか?

rooms [room - 1] + = int.Parse(Console.ReadLine());

namespace BottleDrive 
{ 
    class BottleDrive 
    { 
     public int[] rooms; 
     public BottleDrive() 
     { 
      rooms = new int[4]; 
     } 

     static void Main(string[] args) //static is member of class not object 
     { 
      BottleDrive bD = new BottleDrive(); 
      bD.getBottles(); 
      bD.displayBottleCount(); 
      bD.findWinner(); 
     } 
     public void getBottles() 
     { 
      string quit = Console.ReadLine(); 
      while while(quit != "quit") 
      { 
       int room = int.Parse(quit); 

       Console.Write("Bottles collected in room {0}: ", room); 
       rooms[room - 1] += int.Parse(Console.ReadLine()); 

       Console.Write("Enter the room you're in: "); 
      } 
     } 
     public void findWinner() 
     { 
      int maxValue = 0;//initiates the winner, contructor starts at 0 
      int maxRoomNumber = 0;//initiates the room number that wins 
      for (int i = 0; i < rooms.Length; ++i)//This loop goes through the array of rooms (4) 
      { 
       if (rooms[i] > maxValue)//Makes sure that the maxValue is picked in the array 
       {//Looking for room number for the 
        maxValue = rooms[i]; 
        maxRoomNumber = i + 1; 
       } 
      } 
      Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!"); 
     } 
     public void displayBottleCount() 
     { 
      Console.WriteLine("Bottles collected in room one: " + rooms[0]); 
      Console.WriteLine("Bottles collected in room two: " + rooms[1]); 
      Console.WriteLine("Bottles collected in room three: " + rooms[2]); 
      Console.WriteLine("Bottles collected in room four: " + rooms[3]); 
     } 
    } 
} 
+1

ケーシングが一致していることを確認してください。 'while(quit。ToLower()== "quit") ' – user1231231412

答えて

3

このライン:

while (quit == "quit") 

が実際にする必要があります:いっそ

while (quit != "quit") 

または:入力用ケースを無視する

while (!quit.Equals("quit", StringComparison.CurrentCultureIgnoreCase)) 

。他の人が指摘しているように、あなたのループにはもっと問題があります。あなたのgetBottles機能のためにこれを使用してみてください:

public void getBottles() 
{ 
    string input; 

    do 
    { 
     Console.Write("Enter the room you're in: (or quit)"); 
     input = Console.ReadLine(); 

     int room; 
     // doing try parst because the input might be "quit" or other junk 
     if (int.TryParse(input, out room)) 
     { 
      Console.Write("Bottles collected in room {0}: ", room); 
      // this will fail hard if the input is not of type int 
      rooms[room - 1] += int.Parse(Console.ReadLine()); 
     } 
    } while (!input.Equals("quit", StringComparison.CurrentCultureIgnoreCase)); 
} 
+0

ありがとうございました、ステートメントは行く方法ですし、後に入れ子にします。素晴らしい! – gli

1

while条件にはおそらくwhile(quit != "quit")が必要ですか?

4
while (quit == "quit") 

上記の行は、quit(コンソールから取得したもの)が "quit"の場合にのみループを実行します。

あなたがしたい:あなたもあるものの、すべてのことは、実際にそう一度あなたが出ることはありませんで、あなたのループ内で終了し更新していない

while (quit != "quit") 

または

while (!quit.Equals("quit")) 

後。

あなたのコンソールをキャプチャする必要があります。読んで、 "quit"に入れてください。

終了していない文字列、または有効な整数を入力する場合は、int.TryParseを参照してください。 TryParseは、例外をスローするのではなく、解析が成功したかどうかを示します。

0

試してみてください。

while (!quit.Equals("quit")) 
0

関数getBottles()は非常に奇妙に見えます。 「quit」と入力すると、whileループに入るだけです。私はこれがあなたが望む行動だとは思わない。

+0

ええ、私はちょうどそれを言いました、私は部屋番号を最初に入力して、その部屋が収集しているボトルを入力するようにしたい。 getBottles()は、形式が間違っているか、 部屋のコードです[room - 1] + = int.Parse(Console.ReadLine());違う? – gli

関連する問題