“月,日,年”变量没有存储,我该如何解决?

时间:2022-10-16 10:15:11

I am trying to fix the issues I'm having with getting the data to store in the variables. Whenever I run the program it prints "Invalid Month!, 0, 0" and it wont store anything that I put in the method call in the main method. Not sure what's wrong here.

我正在尝试解决我在将数据存储在变量中时遇到的问题。每当我运行该程序时,它会打印“Invalid Month!,0,0”并且它不会存储我在main方法中放入方法调用的任何内容。不知道这里有什么问题。

    import java.util.*;

    import java.text.*;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Scanner;

    /******************************************************************
     * Back end development for GeoCountDownTimer
     * 
     * @author Jarred
     * @version 9/1/16 
     ******************************************************************/


    public class GeoCountDownTimer {

        /** Variables **/
        public int month, day, year;

        /** Main method to run program **/
        public static void main(String[] args) {
            //Make new object of GeoCountDownTimer to test methods in main
            GeoCountDownTimer g = new GeoCountDownTimer();

            //Creates new object of GeoCountDownTimer
            //Provides input "2/10/2096" but doesn't store correctly
            GeoCountDownTimer timer1 = new GeoCountDownTimer("2/10/2096");

            System.out.println("Date: " + timer1);
        }

        private GeoCountDownTimer(){
            //Instantiate instance variables
            month = 0;
            day = 0;
            year = 0;
        }

        public GeoCountDownTimer(int month, int day, int year){
            //Initializes instance variables with provided values in parameter
            this.month = month;
            this.day = day;
            this.year = year;
        }

        public void GeoCountDownTimer(GeoCountDownTimer other){
            //Initializes variables with other parameter to allow for multiple
            //GeoCountDownTimers to be made
            this.month = other.month;
            this.day = other.day;
            this.year = other.year;

        }
        /******************************************************
         * geoDate takes input in format "dd/MM/yyyy" and I am 
         * attempting to make it function
         * @param geoDate
         *****************************************************/
        public GeoCountDownTimer(String geoDate){

            DateFormat parser = new SimpleDateFormat("dd/MM/yyyy");
            Date date = parser.parse(geoDate);

            int[] numbers = new int[ .length];
            for(int i = 0; i < date.length; i++){
                numbers[i] = Integer.parseInt(date[i]);

            //Potential other option 
            /**
            String[] parts = geoDate.split("/");
            String part1 = parts[0];
            String part2 = parts[1];
            String part3 = parts[2];
            int[] numbers = new int[parts.length];
            for(int i = 0; i < parts.length; i++){
                numbers[i] = Integer.parseInt(parts[i]);

            }
            **/
        }

        public boolean equals(Object other){
            //fix

            }

        }

        public int compareTo(GeoCountDownTimer other){
            //fix
            return day;


        }

        public void dec (int days){
            new GeoCountDownTimer(month, (day - days), year);

        }

        public void dec(){
            new GeoCountDownTimer(month, (day  + 1), year);
        }

        public void inc (int days){
            new GeoCountDownTimer(month, (day + days), year);
        }

        public void inc(){
            new GeoCountDownTimer(month, (day + 1), year);
        } 

        public String toString(){
            String monthString;
            switch(month){

            case 1: monthString = "January";
            break;

            case 2: monthString = "February";
            break;

            case 3: monthString = "March";
            break;

            case 4: monthString = "April";
            break;

            case 5: monthString = "May";
            break;

            case 6: monthString = "June";
            break;

            case 7: monthString = "July";
            break;

            case 8: monthString = "August";
            break;

            case 9: monthString = "September";
            break;

            case 10: monthString = "October";
            break;

            case 11: monthString = "November";
            break;

            case 12: monthString = "December";
            break;

            default: monthString = "Invalid Month!";
            break;
            }

            String result = monthString + " " + day + ", " + year;
            return result;

        }

        public String toDateString(){
            String result = month + " / " + day + " / " + year;
            return result;
        }

        /******************************************************************
         * Getters And Setters
         * @return
         ******************************************************************/
        public int getMonth() {
            return month;
        }

        public void setMonth(int month) {
            this.month = month;
        }

        public int getDay() {
            return day;
        }

        public void setDay(int day) {
            this.day = day;
        }

        public int getYear() {
            return year;
        }

        public void setYear(int year) {
            this.year = year;
        }



    }

1 个解决方案

#1


1  

You are calling the 5th constructor with that String, however in this constructor you never set the month, day, or year of the object. Instead they stay the values they were initialized to, which was 0 for all three values. Then when you go to print the object, the toString method is called, but there is no case in the switch statement for when month=0 so it returns the error.

您正在使用该String调用第5个构造函数,但是在此构造函数中,您永远不会设置对象的月,日或年。相反,他们保留了他们被初始化的值,对于所有三个值都是0。然后当你去打印对象时,会调用toString方法,但是在switch语句中没有case month = 0的情况,所以它返回错误。

To fix this somewhere in that constructor you need to set this.month, this.day, and this.year for the object before trying to print it.

要在该构造函数中的某处修复此问题,您需要在尝试打印之前为该对象设置this.month,this.day和this.year。

#1


1  

You are calling the 5th constructor with that String, however in this constructor you never set the month, day, or year of the object. Instead they stay the values they were initialized to, which was 0 for all three values. Then when you go to print the object, the toString method is called, but there is no case in the switch statement for when month=0 so it returns the error.

您正在使用该String调用第5个构造函数,但是在此构造函数中,您永远不会设置对象的月,日或年。相反,他们保留了他们被初始化的值,对于所有三个值都是0。然后当你去打印对象时,会调用toString方法,但是在switch语句中没有case month = 0的情况,所以它返回错误。

To fix this somewhere in that constructor you need to set this.month, this.day, and this.year for the object before trying to print it.

要在该构造函数中的某处修复此问题,您需要在尝试打印之前为该对象设置this.month,this.day和this.year。