如何拆分用户

时间:2018-01-07 12:54:06

标签: java string split parseint

这是我们在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");
     }
}

3 个答案:

答案 0 :(得分:3)

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返回106而不是7

答案 1 :(得分:0)

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

在您的特定情况下:如果您希望代码按原样运行,则应将ab保留为字符串(不将其解析为整数),然后将mmdd = a + b字符串解析为整数。

答案 2 :(得分:0)

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

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();
            }
        }
    }
}