首页 > 解决方案 > 我被困在这个使用字符串和字符的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;
                    }

                }
            
            
            }

        }
    }
}

标签: java

解决方案


在您的第三个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();
            }

        }

推荐阅读