首页 > 解决方案 > 如何单独访问字符串中的两种不同数据类型?

问题描述

因此,有关更多背景信息,您可以查看我的上一篇文章,但我觉得我问这个问题完全错了。

我得到的是一行上的一个输入:月日,例如 4 月 11 日

我需要做的是,根据给定的月份和日期,确定季节。所以我需要先得到 String inputMonth; 并获取 int inputDay;来自提供两者的单个字符串。我很方便地提供了季节的月份和日期,这导致我写了以下内容:

          //I know that the inputDay is wrong
          inputMonth = scnr.next();
          inputDay = scnr.nextInt();

   if( ((inputMonth == "March") || (inputMonth == "march")) && ((inputDay >= 20) && (inputDay <= 31))){
     System.out.println("Spring");  
  }
   else if( ((inputMonth == "April") || (inputMonth == "april")) && ((inputDay >= 1) && (inputDay <= 30))){
     System.out.println("Spring");  
  }
   else if( ((inputMonth == "May") || (inputMonth == "may")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Spring");  
  }
   else if( ((inputMonth == "June") || (inputMonth == "june")) && ((inputDay >= 1) && (inputDay <= 20))){
     System.out.println("Spring");  
  }
   else if( ((inputMonth == "June") || (inputMonth == "june")) && ((inputDay >= 21) && (inputDay <= 30))){
     System.out.println("Summer");  
  }
   else if( ((inputMonth == "July") || (inputMonth == "july")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Summer");  
  }
   else if( ((inputMonth == "August") || (inputMonth == "august")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Summer");  
  }
   else if( ((inputMonth == "September") || (inputMonth == "september")) && ((inputDay >= 1) && (inputDay <= 21))){
     System.out.println("Summer");  
  }
  else if( ((inputMonth == "September") || (inputMonth == "september")) && ((inputDay >= 22) && (inputDay <= 30))){
     System.out.println("Autumn");  
  }
  else if( ((inputMonth == "October") || (inputMonth == "october")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Autumn");  
  }
  else if( ((inputMonth == "November") || (inputMonth == "november")) && ((inputDay >= 22) && (inputDay <= 30))){
     System.out.println("Autumn");  
  }
  else if( ((inputMonth == "December") || (inputMonth == "december")) && ((inputDay >= 1) && (inputDay <= 20))){
     System.out.println("Autumn");  
  }
  else if( ((inputMonth == "December") || (inputMonth == "december")) && ((inputDay >= 21) && (inputDay <= 31))){
     System.out.println("Winter");  
  }
   else if( ((inputMonth == "January") || (inputMonth == "january")) && ((inputDay >= 1) && (inputDay <= 31))){
     System.out.println("Winter");  
  }
  else if( ((inputMonth == "February") || (inputMonth == "february")) && ((inputDay >= 1) && (inputDay <= 29))){
     System.out.println("Winter");  
  }
  else if( ((inputMonth == "March") || (inputMonth == "march")) && ((inputDay >= 1) && (inputDay <= 19))){
     System.out.println("Winter");  
  }
  else{
   System.out.println("Invalid");  
  }

我的问题是我无法弄清楚如何从那个输入中单独确定月份和日期。每当我运行它时,我总是最终得到“无效”。
我上一篇文章中的一些人展示了不同的方法来解决这个问题,但老实说我还不熟悉数组(这是下一课)。
提前谢谢各位。

标签: javastringinteger

解决方案


推荐阅读