java - 我被困在这个使用字符串和字符的java代码上。我无法通过第一个输入
问题描述
问题:编写一个程序来验证加拿大邮政编码。邮政编码必须遵循 L9L9L9 的模式,其中:
L 是一个字母 9 是一个数字 你的程序应该继续接受邮政编码,直到用户输入单词“exit”。
示例运行(用户输入以粗下划线显示):
输入邮政编码:T2T-3X7
邮政编码长度错误,格式 L9L9L9
输入邮政编码:T2T3AA
邮政编码在应该是数字的地方有字母
输入邮政编码:T2T358
邮政编码在应该是字母的地方有数字
输入邮政编码:T2T3A8
邮政编码有效!
输入邮政编码:退出
我的代码:
// Standard import for the Scanner class
import java.util.*;
public class postalCode {
public static void main (String [] args) {
// Create a Scanner object attached to the keyboard
Scanner input = new Scanner (System.in);
System.out.print("Enter a postal code: ");
String postalCode = input.nextLine();
while (!postalCode.contains("exit")) {
if (postalCode.length() > 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() < 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() == 6) {
for (int i = 0; i < postalCode.length(); i++) {
if (Character.isDigit(postalCode.charAt(0))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(2))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(4))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isLetter(postalCode.charAt(1))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(3))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(5))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
else {
System.out.println("Postal code is Valid!");
break;
}
}
}
}
}
}
解决方案
在您的第三个if{ }
块之后,您需要添加postalCode = input.nextLine();
,以便您下次可以从用户那里获取输入。因此,您在第一次输入后无法继续。
while (!postalCode.contains("exit")) {
if (postalCode.length() > 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() < 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() == 6) {
for (int i = 0; i < postalCode.length(); i++) {
if (Character.isDigit(postalCode.charAt(0))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(2))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(4))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isLetter(postalCode.charAt(1))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(3))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(5))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
else {
System.out.println("Postal code is Valid!");
break;
}
}
postalCode = input.nextLine();
}
}
推荐阅读
- c++ - 深度图像处理 OpenCV
- scala - 带有回调和上下文切换的 scala Future
- javascript - Alfresco 5.2:JavaScript 无法在 IE 中运行
- java - jdbctemplate 查询游标不在第一行之前
- c# - 重定向后的asp.net mvc 401未经授权的错误
- spring - 如何解决 Spring LDAP 中的 ldap 套接字关闭错误?
- c# - Roslyn CodeFix 具有多个要修复的变体
- python - 使用python查找JSON中的缺失值
- google-apps-script - 在相应标题 Google 表格下的编辑移动行
- regex - 从Python中的中文字符串列中删除空格和标点符号