简体   繁体   English

关于else语句(java)

[英]About else statements (java)

So first, the question as follows: Write a program that prompts the user to enter a Social Security number in the format DDD-DD-DDDD, where D is a digit. 首先,问题如下:编写一个程序,提示用户以DDD-DD-DDDD格式输入社会安全号码,其中D是数字。 Your program should check whether the input is valid. 您的程序应检查输入是否有效。 Here are sample runs: 这是示例运行:

Enter a SSN: 232-23-5435

232-23-5435 is a valid Social Security Number. 

Another test: 另一个测试:

Enter a SSN: 23-23-5435

23-23-5435 is an invalid Social Security Number.

My code as follows: 我的代码如下:

import java.util.Scanner;

public class SSN {

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter a Social Security Number in the format DDD-DD-DDDD: ");
    String ssn = input.nextLine();

    boolean correct = true;

    if (ssn.length() != 11) {
      correct = false;
    } else {
      if (!('0' <= ssn.charAt(0) && ssn.charAt(0) <= '9')) {
        correct = false;
      }
      if (!('0' <= ssn.charAt(1) && ssn.charAt(1) <= '9')) {
        correct = false;
      }
      if (!('0' <= ssn.charAt(2) && ssn.charAt(2) <= '9')) {
        correct = false;
      }
      if (ssn.charAt(3) != '-') {
        correct = false;
      }
      if (!('0' <= ssn.charAt(4) && ssn.charAt(4) <= '9')) {
        correct = false;
      }
      if (!('0' <= ssn.charAt(5) && ssn.charAt(5) <= '9')) {
        correct = false;
      }
      if (ssn.charAt(6) != '-') {
        correct = false;
      }
      if (!('0' <= ssn.charAt(7) && ssn.charAt(7) <= '9')) {
        correct = false;
      }
      if (!('0' <= ssn.charAt(8) && ssn.charAt(8) <= '9')) {
        correct = false;
      }
      if (!('0' <= ssn.charAt(9) && ssn.charAt(9) <= '9')) {
        correct = false;
      }
      if (!('0' <= ssn.charAt(10) && ssn.charAt(10) <= '9')) {
        correct = false;
      }
    }

    if (correct == true) {
      System.out.println(ssn + " is a valid Social Security Number.");
    } else {
      System.out.println(ssn + " is an ivalid Social Security Number.");
    }
  }
}

This is a correct answer after asking my professer, though. 不过,这是在问了我的教授之后的正确答案。

However, I still don't understand why I need the else{} between the bunch if statements. 但是,我仍然不明白为什么我需要在if语句之间使用else{}

If I take the else{} away, it works for correct SSNs, but when i enter something invalid, returns: 如果我不使用else{} ,它将适用于正确的SSN,但是当我输入无效内容时,将返回:

Please enter a Social Security Number in the format DDD-DD-DDDD: sd
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    at java.lang.String.charAt(Unknown Source)
    at SSN.main(SSN.java:22)

What's the matter? 怎么了? Why do i need a else there? 为什么在那里需要else Doesn't the program just check every if statements and give the output in the end by checking if correct is correct of false? 程序不是只检查每个if语句,最后通过检查correct是否为false来给出输出吗?

I mean, even without the else, even though there are more calculations, but shouldn't it work the same? 我的意思是,即使没有其他内容,即使有更多的计算,但它不应该一样吗?

I'm confused what went wrong, please help, thanks! 我很困惑,出了什么问题,请帮忙,谢谢!

First if checks if input has correct lenght. 首先if检查输入的长度是否正确。 If not it won't check other conditions. 如果不是,它将不会检查其他条件。

If you remove else , then all conditions will be checked. 如果删除else ,那么将检查所有条件。 So if you have 5 length input you get StringIndexOutOfBoundsException while accessing sixth char. 因此,如果您有5个长度的输入,则在访问第六个字符时会得到StringIndexOutOfBoundsException

You can use Pattern.compile(regex) and then check if regex matches your input instead of writing such a unclean code ifs 您可以使用Pattern.compile(regex) ,然后检查regex是否与您的输入匹配,而不是编写unclean code ifs这样的unclean code ifs

Because you are trying to read a character that doesn't exists. 因为您正在尝试读取一个不存在的字符。 If your input String is shorter than 9 characters the following code will throw an StringIndexOutOfBoundsException 如果您输入的String少于9个字符,则以下代码将引发StringIndexOutOfBoundsException

ssn.charAt(8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM