I';我一直在使用字符串和字符编写java代码。我无法通过第一次输入

I';我一直在使用字符串和字符编写java代码。我无法通过第一次输入,java,Java,问题:编写一个程序来验证加拿大邮政编码。邮政编码必须遵循L9L9L9模式,其中: 我是一封信 9是一个数字 您的程序应继续接受邮政编码,直到用户输入“退出”一词 运行示例(用户输入以粗体下划线显示): 输入邮政编码:T2T-3X7 邮政编码长度错误,格式为L9L9 输入邮政编码:T2T3AA 邮政编码中的字母应该是数字 输入邮政编码:T2T358 邮政编码中应该有字母的地方有数字 输入邮政编码:T2T3A8 邮政编码有效 输入邮政编码:退出 我的代码: // Standard import fo

问题:编写一个程序来验证加拿大邮政编码。邮政编码必须遵循L9L9L9模式,其中:

我是一封信 9是一个数字 您的程序应继续接受邮政编码,直到用户输入“退出”一词

运行示例(用户输入以粗体下划线显示):

输入邮政编码:T2T-3X7

邮政编码长度错误,格式为L9L9

输入邮政编码: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;
                    }

                }
            
            
            }

        }
    }
}
//扫描仪类的标准导入
导入java.util.*;
公共类邮电编码{
公共静态void main(字符串[]args){
//创建连接到键盘的扫描仪对象
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入邮政编码:”);
字符串postalCode=input.nextLine();
而(!postalCode.contains(“exit”)){
如果(postalCode.length()>6){
System.out.println(“邮政编码长度错误,格式为L9L9”);
}
if(postalCode.length()<6){
System.out.println(“邮政编码长度错误,格式为L9L9”);
}
if(postalCode.length()=6){
对于(int i=0;i
您必须在循环内更新
postalCode
。 然后,最好将所有不同的检查提取到单独的方法中,而不是多次打印相同的结果

public class Foo {

    public static void main(String... args) {
        Scanner scan = new Scanner(System.in);

        while (true) {
            System.out.print("Enter a postal code: ");
            String postalCode = scan.nextLine();

            if ("exit".equalsIgnoreCase(postalCode))
                break;

            if (postalCode.length() != 6)
                System.err.println("Postal code wrong length, format L9L9L9");
            else if (!hasDigits(postalCode))
                System.err.println("Postal code has digits where there are supposed to be letters");
            else if (!hasLetters(postalCode))
                System.err.println("Postal code has letters where there are supposed to be digits");
            else
                System.out.println("Postal code is Valid!");
        }
    }

    private static boolean hasDigits(String postalCode) {
        for (int i = 1; i < postalCode.length(); i += 2)
            if (!Character.isDigit(postalCode.charAt(i)))
                return false;
        return true;
    }

    private static boolean hasLetters(String postalCode) {
        for (int i = 0; i < postalCode.length(); i += 2)
            if (!Character.isLetter(postalCode.charAt(i)))
                return false;
        return true;
    }
}
公共类Foo{
公共静态void main(字符串…参数){
扫描仪扫描=新扫描仪(System.in);
while(true){
System.out.print(“输入邮政编码:”);
字符串postalCode=scan.nextLine();
如果(“退出”。相等信号案例(后代码))
打破
如果(postalCode.length()!=6)
System.err.println(“邮政编码长度错误,格式为L9L9”);
如果(!hasDigits(postalCode))
System.err.println(“邮政编码的数字应该是字母”);
如果(!hasLetters(postalCode))
System.err.println(“邮政编码包含字母,其中应该有数字”);
其他的
System.out.println(“邮政编码有效!”);
}
}
专用静态布尔hasDigits(字符串后代码){
对于(int i=1;i
在第三个
之后,如果{}
块需要添加
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();
            }

        }
while(!postalCode.contains(“exit”)){
如果(postalCode.length()>6){
System.out.println(“邮政编码长度错误,格式为L9L9”);
}
if(postalCode.length()<6){
System.out.println(“邮政编码长度错误,格式为L9L9”);
}
if(postalCode.length()=6){
对于(int i=0;i