我被卡在这段使用字符串和字符的Java代码上。我无法通过第一个输入。

huangapple go评论85阅读模式
英文:

I'm stuck on this java code using strings and characters. I cant get past the first input

问题

// 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;
                    }
                }
            }
        }
    }
}
英文:

QUESTION: Write a program to validate Canadian Postal Codes. A postal code must follow the pattern of L9L9L9 where:

L is a letter
9 is a digit
Your program should continue accepting postal codes until the user enters the word “exit”.

Sample run (user input is shown in bold underline):

Enter a postal code: T2T-3X7

Postal code wrong length, format L9L9L9

Enter a postal code: T2T3AA

Postal code has letters where there are supposed to be digits

Enter a postal code: T2T358

Postal code has digits where there are supposed to be letters

Enter a postal code: T2T3A8

Postal code is valid!

Enter a postal code: exit

MY CODE:

// 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(&quot;Enter a postal code: &quot;);
String postalCode = input.nextLine();
while (!postalCode.contains(&quot;exit&quot;)) {
if (postalCode.length() &gt; 6){
System.out.println(&quot;Postal code wrong length, format L9L9L9&quot;);
}
if (postalCode.length() &lt; 6){
System.out.println(&quot;Postal code wrong length, format L9L9L9&quot;);
}
if (postalCode.length() == 6) {
for (int i = 0; i &lt; postalCode.length(); i++) {
if (Character.isDigit(postalCode.charAt(0))){
System.out.println(&quot;Postal code has digits where there are supposed to be letters&quot;);
break;
}
if (Character.isDigit(postalCode.charAt(2))){
System.out.println(&quot;Postal code has digits where there are supposed to be letters&quot;);
break;
}
if (Character.isDigit(postalCode.charAt(4))){
System.out.println(&quot;Postal code has digits where there are supposed to be letters&quot;);
break;
}
if (Character.isLetter(postalCode.charAt(1))){
System.out.println(&quot;Postal code has letters where there are supposed to be digits&quot;);
break;
}
if (Character.isLetter(postalCode.charAt(3))){
System.out.println(&quot;Postal code has letters where there are supposed to be digits&quot;);
break;
}
if (Character.isLetter(postalCode.charAt(5))){
System.out.println(&quot;Postal code has letters where there are supposed to be digits&quot;);
break;
}
else {
System.out.println(&quot;Postal code is Valid!&quot;);
break;
}
}
}
}
}
}

答案1

得分: 1

在第三个 if{ } 代码块之后,您需要添加 postalCode = input.nextLine(); 以便下次可以从用户那里获取输入。因为缺少这部分,您无法在第一次输入后继续执行。

while (!postalCode.contains("exit")) {
    if (postalCode.length() > 6) {
        System.out.println("邮政编码长度错误,格式应为 L9L9L9");
    }
    if (postalCode.length() < 6) {
        System.out.println("邮政编码长度错误,格式应为 L9L9L9");
    }
    if (postalCode.length() == 6) {
        for (int i = 0; i < postalCode.length(); i++) {
            if (Character.isDigit(postalCode.charAt(0))) {
                System.out.println("邮政编码有数字,但应为字母");
                break;
            }
            if (Character.isDigit(postalCode.charAt(2))) {
                System.out.println("邮政编码有数字,但应为字母");
                break;
            }
            if (Character.isDigit(postalCode.charAt(4))) {
                System.out.println("邮政编码有数字,但应为字母");
                break;
            }
            if (Character.isLetter(postalCode.charAt(1))) {
                System.out.println("邮政编码有字母,但应为数字");
                break;
            }
            if (Character.isLetter(postalCode.charAt(3))) {
                System.out.println("邮政编码有字母,但应为数字");
                break;
            }
            if (Character.isLetter(postalCode.charAt(5))) {
                System.out.println("邮政编码有字母,但应为数字");
                break;
            } else {
                System.out.println("邮政编码有效!");
                break;
            }
        }
        postalCode = input.nextLine();
    }
}
英文:

After your third if{ } block you need to add postalCode = input.nextLine(); so that you can take the input from user next time. Because of this you are not able to proceed after first input.

while (!postalCode.contains(&quot;exit&quot;)) {
if (postalCode.length() &gt; 6){
System.out.println(&quot;Postal code wrong length, format L9L9L9&quot;);
}
if (postalCode.length() &lt; 6){
System.out.println(&quot;Postal code wrong length, format L9L9L9&quot;);
}
if (postalCode.length() == 6) {
for (int i = 0; i &lt; postalCode.length(); i++) {
if (Character.isDigit(postalCode.charAt(0))){
System.out.println(&quot;Postal code has digits where there are supposed to be letters&quot;);
break;
}
if (Character.isDigit(postalCode.charAt(2))){
System.out.println(&quot;Postal code has digits where there are supposed to be letters&quot;);
break;
}
if (Character.isDigit(postalCode.charAt(4))){
System.out.println(&quot;Postal code has digits where there are supposed to be letters&quot;);
break;
}
if (Character.isLetter(postalCode.charAt(1))){
System.out.println(&quot;Postal code has letters where there are supposed to be digits&quot;);
break;
}
if (Character.isLetter(postalCode.charAt(3))){
System.out.println(&quot;Postal code has letters where there are supposed to be digits&quot;);
break;
}
if (Character.isLetter(postalCode.charAt(5))){
System.out.println(&quot;Postal code has letters where there are supposed to be digits&quot;);
break;
}
else {
System.out.println(&quot;Postal code is Valid!&quot;);
break;
}
}
postalCode = input.nextLine();
}
}

答案2

得分: 0

你必须在循环内更新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;
    }
}
英文:

You have to update postalCode inside a loop.
Then it's better to extract all different checks to the separate methods instead of printing same result multiple times.

public class Foo {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print(&quot;Enter a postal code: &quot;);
String postalCode = scan.nextLine();
if (&quot;exit&quot;.equalsIgnoreCase(postalCode))
break;
if (postalCode.length() != 6)
System.err.println(&quot;Postal code wrong length, format L9L9L9&quot;);
else if (!hasDigits(postalCode))
System.err.println(&quot;Postal code has digits where there are supposed to be letters&quot;);
else if (!hasLetters(postalCode))
System.err.println(&quot;Postal code has letters where there are supposed to be digits&quot;);
else
System.out.println(&quot;Postal code is Valid!&quot;);
}
}
private static boolean hasDigits(String postalCode) {
for (int i = 1; i &lt; 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 &lt; postalCode.length(); i += 2)
if (!Character.isLetter(postalCode.charAt(i)))
return false;
return true;
}
}

huangapple
  • 本文由 发表于 2020年10月26日 15:58:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64533292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定