首页 > 解决方案 > 如何让我的程序识别给定字符串中的字符串和整数?

问题描述

所以这是作业问题:

“编写一个程序,将日期作为输入并输出日期的季节。输入是一个表示月份的字符串和一个表示日期的 int。

例如:如果输入是:

4 月 11 日的输出是:

Spring 另外,检查 string 和 int 是否有效(实际的月份和日期)。

例如:如果输入是:

蓝色 65 输出为:

无效的 ”

我的代码如下:

'''

    String inputMonth;
    int inputDay;

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

'''

我认为问题在于它不会正确读取字符串和整数,但我不确定为什么。

另外我知道可能有更短的方法可以做到这一点,但我不知道如何,如果有人也愿意帮助我,我将不胜感激。

先感谢您

标签: javastringintegeruser-input

解决方案


我不知道这个版本对你是否更有意义。

我将月份名称、季节和日期范围放在数组中。通常,我会创建一个类来保存季节名称和日期范围,但我使用数组来使其更简单。

我评论了更晦涩的代码部分。我希望这有帮助。

package com.ggl.testing;

import java.util.Scanner;

public class Seasons {

    static String[] months = { "January", "February", "March", "April", 
            "May", "June", "July", "August", "September",
            "October", "November", "December" };

    // Winter has two ranges to make the comparisons easier
    static String[] seasons = { "Spring", "Summer", "Autumn", "Winter", 
            "Winter" };

    // Month and day to start the season, month and day to end the season
    // Winter has two ranges to make the comparisons easier
    // The month number is zero based (0 - 11)
    static int[][] ranges = { { 2, 20, 5, 20 }, { 5, 21, 8, 21 }, 
            { 8, 22, 11, 20 }, { 11, 21, 11, 31 },
            { 0, 1, 2, 19 } };

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] values = getInput(scanner);
        if (values[0] < 0 || values[1] < 0) {
            System.out.println("Invalid");
        } else {
            System.out.println(getSeason(values));
        }
        scanner.close();
    }

    static int[] getInput(Scanner scanner) {
        int[] output = new int[2];
        System.out.print("Type the month and day: ");
        String s = scanner.nextLine().trim();
        String[] parts = s.split(" ");
        output[0] = getMonthIndex(parts[0]);
        output[1] = getDay(parts[1]);
        return output;
    }

    static int getMonthIndex(String month) {
        for (int i = 0; i < months.length; i++) {
            if (month.toLowerCase().equals(months[i].toLowerCase())) {
                return i;
            }
        }
        return -1;
    }

    static int getDay(String number) {
        try {
            int value = Integer.valueOf(number);
            // TODO Check last day of a particular month
            if (value < 1 || value > 31) {
                return -1;
            } else {
                return value;
            }
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    static String getSeason(int[] values) {
        for (int i = 0; i < ranges.length; i++) {
            if ((ranges[i][0] == values[0]) && (ranges[i][1] <= values[1])) {
                return seasons[i];
            } else if ((values[0] == ranges[i][2]) && (values[1] <= ranges[i][3])) {
                return seasons[i];
            } else if ((ranges[i][0] < values[0]) && values[0] < ranges[i][2]) {
                return seasons[i];
            }
        }
        return "Invalid";
    }

}

推荐阅读