私のプログラムは、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]);
}
}
}
ケーシングが一致していることを確認してください。 'while(quit。ToLower()== "quit") ' – user1231231412