在Java中获得两个返回值



i具有以下代码。我需要从" getInput"方法返回一个月和年的值。将错误作为无法达到的语句。我正在使用BlueJ IDE。如何获得拖曳返回值。请帮忙。

public class CalendarTester
{
    public static void main(String[] args) { //method call for testing valid inputs from the user
       nputValidate();
    }
    public static void InputValidate(){ //Method to call functions for validation of inputs
        String UserInput="";
        UserInput=getInput();
        ValidateInput(UserInput);
    }
    public static String getInput(){ // To read user input
        Scanner scanner=new Scanner(System.in);
        System.out.println("Please enter the year (eg - 2016):");
        String year = scanner.next();
        System.out.println("Please enter the month (eg - 10):");
        String month = scanner.next();
        return year;
        return month;
    }
    public static boolean ValidateInput(String toValidation){ // Method to validate inputs
        boolean Pass=false;
        String finalString=toValidation.replaceAll("\s+","");
        String matchingString="[0-9]{4,6}";
        if(finalString.matches(matchingString)){
            String Month=finalString.substring(0, 3);
            String Year = finalString.substring(0, 4);
            int month=Integer.parseInt(Month);
            int year=Integer.parseInt(Year);
            if(month>0 && month<=12){
                if(year>999 & year<=10000){
                    Pass=true;
                    Calendar calender = new Calendar();
                    boolean isLeapYear=calender.isLeapYear((short) year);
                    if(isLeapYear){
                        System.out.println("The given year " +year+ " is a leap Year");
                    }else{
                        System.out.println("The given year " +year+ " is not a leap Year");
                    }
                    byte TotalDaysInMonth=calender.TotalDaysOfMonth((byte) month,(short) year);
                    System.out.println("Total days in the month " +month+ "are "+TotalDaysInMonth);
                    byte week=calender.firstDayOfYear((short) year);
                    String FirstDayOfWeek ="";
                    switch (week) { //Case for first day of the week
                    case 0:
                        FirstDayOfWeek="Mon";
                        break;
                    case 1:
                        FirstDayOfWeek="Tue";
                        break;
                    case 2:
                        FirstDayOfWeek="Wed";
                        break;
                    case 3:
                        FirstDayOfWeek="Thur";
                        break;
                    case 4:
                        FirstDayOfWeek="Fri";
                        break;
                    case 5:
                        FirstDayOfWeek="Sat";
                        break;
                    case 6:
                        FirstDayOfWeek="Sun";
                        break;
                    default:
                        FirstDayOfWeek="Invalid week input";
                        break;
                    }
                    System.out.println("The first day of the year"+year+"is "+FirstDayOfWeek);
                    byte firstmonthday = calender.firstDayOfMonth((byte) month,(short) year);
                    String dayName = "";
                    switch(firstmonthday)//to print the first day of the month
                    {
                        case 0: dayName = "Sat"; break;
                        case 1: dayName = "Sun"; break;
                        case 2: dayName = "Mon"; break;
                        case 3: dayName = "Tue"; break;
                        case 4: dayName = "Wed"; break;
                        case 5: dayName = "Thur"; break;
                        default: dayName = "Fri"; break;
                    }
                    System.out.println("The first day of the month" +month+ "is " + dayName);
                    calender.printMonth((byte) month,(short) year);
                }else{
                    System.out.println("Invalid year input");
                    Pass=false;
                    InputValidate();
                }
            }
            else if(month<=0 || month>12){ //validates the month entered
                System.out.println("Invalid month input");
                Pass=false;
                InputValidate();
            }
        }
        return Pass;
    }
}

这样做的Java-Idiomic方法是将一个新的班级制作,其中包含要返回的成员的东西:

public static class Foo
{
    String year;
    String month;
}

return an 实例

您会发现这是 scales 最好的方法。最终,您最终将在此class中添加各种其他功能。

您甚至可能注意到它与某些标准类(java.util.Date和较新的java.time.LocalDate)太相似,然后完全放弃整个过程。

返回 1 array 包含 2值

尝试使用数组,您可以分配2个或更多值:

public static String[] getInput(){ // To read user input
Scanner scanner=new Scanner(System.in);
System.out.println("Please enter the year (eg - 2016):");
String year = scanner.next();
System.out.println("Please enter the month (eg - 10):");
String month = scanner.next();
return new String[]{year,month};
}

最新更新