我在此問題陳述中顯示了一部分輸入代碼。我想從用戶那裏只輸入字母。在這裏,我想遍歷這個方法,直到用戶以字母輸入。 getInput是我班上的方法。在該方法中迭代,直到用戶僅以字母輸入提供輸入
public string getInput()
{
Console.WriteLine("Please enter your name. If you want to send the parcel: ");
this.NameOfSender = Console.ReadLine();
return NameOfSender;
}
在這裏,我想,如果用戶輸入錯誤輸入此代碼應打印信息「輸入錯誤。請輸入有效的名稱。」然後再次啓動該方法。請幫助我如何做到這一點。
創建一個布爾變量,並將其設置爲false。當變量爲false時循環。如果輸入有效,請將其設置爲true。 –
查找用於字母數字條目的RegEx。將ReadLine與正則表達式匹配。如果通過,請寫下「謝謝」。如果不是,請寫下「必須爲字母」,然後再次調用getInput –
@DStanley請您爲我提供一些代碼。要麼我需要使用while循環?或者是其他東西。如果你可以提供我的代碼,我會感謝你。 – mac