【问题标题】:How do I get my program to identify a the string and integer within a given string? [duplicate]如何让我的程序识别给定字符串中的字符串和整数? [复制]
【发布时间】:2020-03-17 19:56:14
【问题描述】:

这是作业问题:

"编写一个程序,以日期为输入,输出日期的季节。输入是一个字符串表示月份,一个int表示日期。

例如:如果输入是:

4 月 11 日 输出是:

春天 另外,检查 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");  
  }

'''

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

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

提前谢谢你

【问题讨论】:

  • 另外,我认为你误解了你的任务。该示例在一行中显示月份和日期,但您的程序似乎希望单独输入这些值。在这种情况下,您应该发布第二个问题,因为答案将与这个问题大不相同。
  • 查看 String 类的 Java API 并寻找不同类型的 equals 方法。这将使您的任务更轻松。
  • 原发帖者只是求助,我不知道为什么这些问题无法回答以帮助教育人们。该操作使用对象身份的等效性,即存储在内存中的哈希值。要修复代码,您需要比较字符串值。而不是 inputMonth == "april" 它应该是 inputMonth.equals("april") 我还建议在输入上做 toLowerCase

标签: java string integer user-input


【解决方案1】:

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

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

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

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 2018-03-05
    • 2022-08-18
    • 2021-02-11
    • 1970-01-01
    相关资源
    最近更新 更多