我必须创建一个循环,要求用户从上至下输入第一个数字,如果不满足要求,则用户必须再次输入,直到第一个字母在上而其他字母在下
var novaDrzava = new Država();
Console.Write("Vnesite ime (prva začetnica naj bo velika, ostale male): ");
novaDrzava.Ime = Console.ReadLine();
var drzava = novaDrzava.Ime;
var inicialka = drzava.Substring(0);
var ostale = drzava.Substring(1, drzava.Length - 1);
for (int i = 0; i <= malecrke.Length; i++) {
if (inicialka.Contains(velikecrke[i])) {
if (ostale.Contains(malecrke[i])) {
break;
} else {
Console.WriteLine("Ponovno vnesite ime");
novaDrzava.Ime = Console.ReadLine();
}
}
}
答案 0 :(得分:0)
保持循环,直到novaDrzava.Ime
有效:
using System.Text.RegularExpressions;
...
Console.Write("Vnesite ime (prva začetnica naj bo velika, ostale male): ");
// Keep looping
while (true) {
// Get a new user input
// .Normalize() - let diacritics be represent in a standard way
// .Trim() - let be nice and remove leading/trailinf white spaces
novaDrzava.Ime = Console.ReadLine().Normalize().Trim();
// If input is valid one (uppercase followed zero or more lowercase)
// we can stop looping
if (Regex.IsMatch(novaDrzava.Ime, @"^\p{Lu}\p{Ll}*$")
break;
// input is not valid: let user know it and keep asking
Console.WriteLine("Ponovno vnesite ime");
}
// From now on novaDrzava.Ime contains a valid name
答案 1 :(得分:0)
为什么不接受他们的输入并按照您需要的方式对其进行格式化?这将消除对循环的需要。像这样:
OnKeyDown
答案 2 :(得分:-3)
一个简单的getline
就足够了,例如C&C ++
只是在内部使用ifs来检查他已经放入的数据,如果满足要求就去检查新数据。
否则,如果他在关键时刻输入了错误的数据,请再次询问他并重置他已经输入的数据。
这是在C中工作的示例,因此您应该能够在C#中复制它 稍作修改,一次输入一个字符。
#include <stdio.h>
#include <stdlib.h>
int is_upper(char *str)
{
int idx = 0;
if (str[idx] >= 'a' && str[idx] <= 'z') {
return (0);
}
if (str[idx] >= 'A' && str[idx] <= 'Z') {
return (1);
}
return (2);
}
int main(void)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
int mystrlen = 0;
char finalstring[2048]; //to ease the algorithm so there is no need to do a dynamic allocation due to C behavior
while ((read = getline(&line, &len, stdin)) != -1) {
if (strlen(line) == 2) { // because \n == 1 char
if (mystrlen == 0 && line[0] >= 'A' && line[0] <= 'Z') {
strcat(finalstring, line);
mystrlen++; //first letter = UPPER CASE
}
else if (is_upper(line) == 2 || is_upper(line) == 1 || mystrlen == 0) { //if first letter is good & we aren't at first letter, or simply bad char input
mystrlen = 0;
finalstring[0] = '\0';
printf("reset\n");
}
else if (is_upper(line) == 0) { //when all is good
strcat(finalstring, line);
printf("\n%s\n", finalstring);
}
}
else {
mystrlen = 0;
finalstring[0] = '\0';
printf("reset\n");
}
}
}