do{
out.println("\n---------------------------------");
out.println("---------------------------------");
out.print("Please type your acces card number: ");
try{
card = input.nextInt();
if(card.length != 10){
out.println("The number you typed is incorrect");
out.println("The number must be 10 numbers long");
continue;
}
}
catch(InputMismatchException ex){
}
}while(true);
林試圖使卡是10個字符長。像(1234567890),並且如果用戶輸入(123),還是應該出現(123456789098723)的錯誤消息。 card.length似乎沒有工作。我如何可以設置多少個字符可以輸入的限制? (JAVA)
也許你可以嘗試接收純字符串,然後解析爲整數。 – HuStmpHrrr 2014-12-02 02:23:28
我贊同「將卡號視爲字符串」的方法 - 通常卡號不需要對它們進行數學運算,並且adn可以包含前導零,因此使用字符串實際上是一種明智的表示形式。 – 2014-12-02 02:27:07