![](/img/trans.png)
[英]How do I overload function to accept both async and synchronized version of callback parameter
[英]How do I overload this code to make it accept my entry?
我正在開發一個程序來收集“瓶驅動器”的結果。 該程序將提示用戶輸入1到4之間的房間號,然后提示用戶輸入每個房間收集的瓶子數。 用戶應該可以選擇鍵入“退出”以顯示每個房間和收集的瓶子數量。
這是我現在的代碼,我很難擴展程序以支持多個房間。
namespace BottleDrive
{
class Program
{
static void Main(string[] args)
{
int roomNumber;
int numberOfBottles;
char quit;
Console.WriteLine("Enter the Room number you are in.");
string roomSelect = "";
roomSelect = Console.ReadLine();
roomSelect = int.Parse(roomSelect);
if (roomSelect >= 1)
{
Console.WriteLine("Enter the number of bottles collected by room one");
string room1Bottles = "0";
room1Bottles = Console.ReadLine();
int room1 = int.Parse(room1Bottles);
if (room1 == 1)
{
room1Bottles += room1;
Console.WriteLine("Room one has " + room1 + " many bottles collected");
}
}
if (Console.ReadLine() = quit)
{
Console.WriteLine("Room one has collected:" + room1Bottles + "\nRoom two has collected:" + room2Bottles + "Press space to quit");
string systemExit = "";
if (Console.ReadLine = "")
{
systemExit(0);
}
}
您可以利用一個數組(房間號將用於計算索引)來跟蹤收集的瓶子數量:
int[] bottles = new int[4];
while (true)
{
Console.Write("Enter the room you're in: ");
string inputString = Console.ReadLine();
if (inputString == "quit")
break;
int room = int.Parse(inputString);
Console.Write("Bottles collected in room {0}: ", room);
bottles[room - 1] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < bottles.Length; ++i)
Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, bottles[i]);
然后,一旦用戶鍵入“退出”以顯示收集了多少瓶,您就可以再次遍歷數組。
我懷疑您的代碼僅是因為以下原因而編譯:
string roomSelect = "";
roomSelect = Console.ReadLine();
roomSelect = int.Parse(roomSelect);
使用整數變量存儲已解析的房間號。
因為您只需要知道每個房間一個變量,所以可以使用整數數組,在這種情況下,該數組包含4個條目。 您將從用戶那里獲取輸入並將其轉換為int。 然后,您可以使用該數字訪問正確的數組索引。
int[] arr_rooms = new int[4];
Console.WriteLine("Enter room number");
int number = Int.Parse(Console.ReadLine());
Console.WriteLine("enter number of bottles");
int bottles = Int.Parse(Console.ReadLine());
arr_rooms[number - 1] = bottles; //the -1 is because arrays start at 0
Console.WriteLine("bottles in room 1 is " + arr_rooms[0].toString());
您將必須聲明一個數組來存儲瓶數:
static void Main(string[] args)
{
const int NumberOfRooms = 4;
int[] numberOfBottles = new int[NumberOfRooms];
while (true) {
Console.WriteLine("Enter the Room number you are in or 'q' and <enter> to quit.");
string line = Console.ReadLine();
if (line.ToLower().StartsWith("q")) {
break;
}
int roomNumber = int.Parse(line) - 1;
if (roomNumber >= 0 && roomNumber < NumberOfRooms) {
Console.WriteLine("Enter the number of bottles collected by room number " + (roomNumber + 1));
line = Console.ReadLine();
int bottles = int.Parse(line);
numberOfBottles[roomNumber] += bottles;
Console.WriteLine("{0} bottles collected for room number {1}", numberOfBottles[roomNumber], roomNumber + 1);
Console.WriteLine();
}
}
Console.WriteLine();
for (int i = 0; i < NumberOfRooms; i++) {
Console.WriteLine("{0} bottles collected for room number {1}", numberOfBottles[i], i + 1);
}
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
還要注意,數組的索引從0到項數減一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.