Java 如何让程序识别给定字符串中的字符串和整数?

Java 如何让程序识别给定字符串中的字符串和整数?,java,string,integer,user-input,Java,String,Integer,User Input,这是家庭作业的问题: “编写一个程序,将日期作为输入并输出日期的季节。输入是一个表示月份的字符串和一个表示日期的整数 例如:如果输入为: 四月十一日 输出为: 弹簧 此外,检查字符串和int是否有效(实际的月份和日期) 例如:如果输入为: 蓝色65 输出为: 无效的 " 我的代码如下: ''' 字符串输入月份; 国际输入日; inputMonth=scnr.next(); inputDay=snr.nextInt(); 如果((输入月份=“三月”)| |(输入月份=“三月”)&((输入日>=20

这是家庭作业的问题:

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

例如:如果输入为:

四月十一日 输出为:

弹簧 此外,检查字符串和int是否有效(实际的月份和日期)

例如:如果输入为:

蓝色65 输出为:

无效的 "

我的代码如下:

'''

字符串输入月份;
国际输入日;
inputMonth=scnr.next();
inputDay=snr.nextInt();

如果((输入月份=“三月”)| |(输入月份=“三月”)&((输入日>=20)&(输入日=1)&(输入日=1)&(输入日=1)&(输入日=21)&(输入日=1)&(输入日=1)&(输入日=1)&(输入日=1)&(输入日=22)&(输入日=1)&(输入日=22)&(输入日=1)&(输入日=21)&(输入日=1)&(输入日=1)&(输入日=1)&(输入日=1)&(输入日=1)&(输入日=1)&)(inputDay=1)&&(inputDay我不知道这个版本对您是否更有意义

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

我对代码中比较晦涩的部分进行了评论。我希望这能有所帮助

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

}
package com.ggl.testing;
导入java.util.Scanner;
公共课季节{
静态字符串[]月={“一月”、“二月”、“三月”、“四月”,
“五月”、“六月”、“七月”、“八月”、“九月”,
“十月”、“十一月”、“十二月”};
//冬季有两个范围,便于比较
静态字符串[]季节={“春”、“夏”、“秋”、“冬”,
“冬天”};
//开始季节的月份和日期,结束季节的月份和日期
//冬季有两个范围,便于比较
//月数以零为基(0-11)
静态int[][]范围={{2,20,5,20},{5,21,8,21},
{ 8, 22, 11, 20 }, { 11, 21, 11, 31 },
{ 0, 1, 2, 19 } };
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
int[]值=getInput(扫描仪);
如果(值[0]<0 | |值[1]<0){
System.out.println(“无效”);
}否则{
System.out.println(getseasure(value));
}
scanner.close();
}
静态int[]getInput(扫描仪){
int[]输出=新的int[2];
System.out.print(“键入月份和日期:”);
字符串s=scanner.nextLine().trim();
字符串[]部分=s.split(“”);
输出[0]=getMonthIndex(部分[0]);
输出[1]=getDay(部分[1]);
返回输出;
}
静态int getMonthIndex(字符串月){
对于(int i=0;i31){
返回-1;
}否则{
返回值;
}
}捕获(数字格式){
返回-1;
}
}
静态字符串getSeason(int[]值){
对于(int i=0;i如果((范围[i][0]==值[0])&(范围[i][1]另外,我认为您误解了您的赋值。示例在一行上显示了月份和日期,但您的程序似乎希望分别输入值。在这种情况下,您应该发布第二个问题,因为答案与此问题有很大不同。请查看String类的Java API并查找dif不同类型的
等于
方法。这将使您的任务更简单。原始海报只是寻求帮助,我不知道为什么这些问题无法回答来帮助教育人们。op使用的是对象标识的等效性,即存储在内存中的哈希值。要修复代码,需要比较字符串值。instinputMonth的ead==“april”应该是inputMonth.equals(“april”)我还建议对输入进行小写
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";
    }

}