如何拆分用户输入的字符串

How to split the string inputted by user

这是我们在 Java 编程中的作业。我想知道如何拆分用户输入的字符串。
该程序将要求用户以 MM/dd 格式输入他的出生日期。之后我希望应用程序拆分日期(分隔符是斜杠)。添加号码。并根据数字的总和输出十二生肖。

预期结果:

Please enter your date of birth (MM/dd): 01/06
This is the mmdd value: 106
Result: Capricorn

但是我没有用下面的代码得到这个结果:

public static void main(String[] args) {

    Scanner scan = new Scanner (System.in);
    int mmdd;
    int a;
    int b;

   System.out.print("Please enter your date of birth(MM/dd): ");
   String stringdate = scan.next();

   a = Integer.parseInt(stringdate.split("/") [0]);
   b = Integer.parseInt(stringdate.split("/") [1]);
   mmdd = a + b;

   System.out.println("This is the mmdd value: " + mmdd);

   System.out.print("Result: ");
     if (mmdd >= 321 && mmdd <= 419) {
         System.out.println("ARIES");
     } else if (mmdd >= 420 && mmdd <= 520) {
         System.out.println("TAURUS");
     } else if (mmdd >= 521 && mmdd <= 620) {
         System.out.println("GEMINI");
     } else if (mmdd >= 621 && mmdd <= 722) {
         System.out.println("CANCER");
     } else if (mmdd >= 723 && mmdd <= 822) {
         System.out.println("LEO");
     } else if (mmdd >= 823 && mmdd <= 922) {
         System.out.println("VIRGO");
     } else if (mmdd >= 923 && mmdd <= 1022) {
         System.out.println("LIBRA");
     } else if (mmdd >= 1023 && mmdd <= 1121) {
         System.out.println("SCORPIO");
     } else if (mmdd >= 1122 && mmdd <= 1221) {
         System.out.println("SAGITTARIUS");
     } else if ((mmdd >= 1222 && mmdd <= 1231) || (mmdd >= 11 && mmdd <= 119)) {
         System.out.println("CAPRICORN");
     } else if (mmdd >= 120 && mmdd <= 218) {
         System.out.println("AQUARIUS");
     } else if (mmdd >= 219 && mmdd <= 320) {
         System.out.println("PISCES");
     }
}

a = Integer.parseInt(stringdate.split("/") [0]); 给你 1
b = Integer.parseInt(stringdate.split("/") [1]); 给你 6

所以实际上你在求和 1 + 6 得到 7 而不是 106

您可以使用 :

String stringdate = scan.next();
stringdate = stringdate.replaceAll("[^\d]+", "");//remove all non digits

mmdd = Integer.parseInt(stringdate);//then parse the result

如果您输入 01/06 那么 mmdd return 106 而不是 7

您将整数的 sum 运算符与字符串的 concat 运算符混淆了。 它们可能看起来相同 (+) 但它们的行为却大不相同。

在您的特定情况下:如果您希望代码按原样工作,您应该将 ab 保留为字符串(不解析它们转化为整数),然后将 mmdd = a + b 字符串解析为整数。

您的代码可以重新格式化为

public class Main{

    public static void main( String[] args ){
        LocalDate parse = LocalDate.parse( "11/15" , DateTimeFormatter.ofPattern( "MM/dd" ) );
//        month * 100 to make your function( 01/06 ) = 106
        int sum = parse.getMonthValue() * 100 + parse.getDayOfMonth();
        System.out.println( sum );
        System.out.println( Zodiac.valueFromSum( sum ) );
    }

    enum Zodiac{
        ARIES,
        TAURUS,
        GEMINI,
        CANCER,
        LEO,
        VIRGO,
        LIBRA,
        SCORPIO,
        SAGITTARIUS,
        CAPRICORN,
        AQUARIUS,
        PISCES;

        static Zodiac valueFromSum( Integer mmdd ){
            if( mmdd >= 321 && mmdd <= 419 ){
                return ARIES;
            }else if( mmdd >= 420 && mmdd <= 520 ){
                return TAURUS;
            }else if( mmdd >= 521 && mmdd <= 620 ){
                return GEMINI;
            }else if( mmdd >= 621 && mmdd <= 722 ){
                return CANCER;
            }else if( mmdd >= 723 && mmdd <= 822 ){
                return LEO;
            }else if( mmdd >= 823 && mmdd <= 922 ){
                return VIRGO;
            }else if( mmdd >= 923 && mmdd <= 1022 ){
                return LIBRA;
            }else if( mmdd >= 1023 && mmdd <= 1121 ){
                return SCORPIO;
            }else if( mmdd >= 1122 && mmdd <= 1221 ){
                return SAGITTARIUS;
            }else if( ( mmdd >= 1222 && mmdd <= 1231 ) || ( mmdd >= 11 && mmdd <= 119 ) ){
                return CAPRICORN;
            }else if( mmdd >= 120 && mmdd <= 218 ){
                return AQUARIUS;
            }else if( mmdd >= 219 && mmdd <= 320 ){
                return PISCES;
            }else{
                throw new IllegalArgumentException();
            }
        }
    }
}