1
我需要两个输入一个int值。用户可以在1月份写入1或01。
所以它不工作了1 - 12,但如果我写01它确实给了我自己的错误文本。
对我来说,它的importent,它是一个int值,我知道该字符串被很好地工作。验证月份输入01和1?
private static BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
private Output output = new Output();
public static String inputText() throws Exception {
return input.readLine();
}
public int monatInput(String text){
String monthNumber= "";
boolean again = true;
while (again) {
System.out.print(text);
again = false;
try {
monthNumber = inputText();
int number= Integer.parseInt(monatZahl);
int monthLength = String.valueOf(monatZahl).length();
if (number<=1 || number>=12) {
again = true;
throw new Exception();
} else if (monthLength> 3) {
again = true;
throw new Exception();
}
} catch (Exception e) {
this.output.monatWrongInput;
}
}
int converter = Integer.parseInt(monatZahl);
return converter;
}
我希望你们能帮我验证这个问题。我看过很多关于for循环的文章,以生成01位数字,但这有点不同,然后验证为用户输入。
我所需要的长度,因为如果我尝试键入01,它抛出我的新例外。对不起,我只是想翻译我的程序。但它仍然不工作,我不能01 - 12。它适用于1-12,但我需要这两个功能。我希望我能让我的问题变得清晰。 –
@ MonsterHunter.Dev它会让你的例外,因为你不使用正确的输入另一件事检查我的编辑 –
阅读我的答案仔细@ MonsterHunter.Dev你必须检查与'如果(数<1张||数> 12){ '而不是用'如果(数量<= 1 ||数> = 12){'它将如果你使用'01'这是逻辑 –