在main和Java之间传递信息

时间:2012-10-29 01:45:49

标签: java

坚持我的最后一个项目,要么忘记了如何来回传递数据,要么我只是遗漏了一些东西。真的很喜欢暗示或回答谢谢!

package coutingdays;
import coutingdays.day.Day;
import java.util.*;

public class CoutingDays {


    public static void main(String[] args)
    {
        // declare the scanner for the keyboard
        Scanner kb = new Scanner(System.in);
        int farDay;
                String userDay;
        // create the Day object
        Day theDay = new theDay();
        userDay = "";
        farDay = 0;

        // get the users day
        System.out.println("Enter today's Day in caps: ");
        userDay = kb.next();

        System.out.println("Enter the number of days from today to calculate: ");
        farDay = kb.nextInt();

        // pass the input to the object 
        theDay.setDay(userDay, farDay);


        // print the results
        theDay.printDay();



    }
}

课日

public class Day
{

    // declare the private variables.
    private String toDay;
    private String toMorrow;
    private String yesterDay;
    private String laterDay;
    private int d = 0;
    private int e;




    // create array of the days of the week
    private String[] weekDays = {"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"};

    // create a constructor to initialize the object to zero
    public Day()
    {
        toDay = "";
        toMorrow = "";
        yesterDay = "";
        laterDay = "";
        e = 0;

    }

    // define the setDay method that will calculate today's day,
    // tomorrow's day, yesterday's day, and a day in the future
    public void setDay(String userDay, int farDay)
    {
        //declare variables to be used by the method
        toMorrow = userDay;
        yesterDay = userDay;
        laterDay = userDay;
        e = farDay;
        int x = 0;
        int y = 0;
        int z = 0;

        // print today's day based on user input
        System.out.println("Today is: " + userDay);

        // loop through the array to match day entered by user
        // then add one to return array value of tomorrow
        for (x = 0; x<7; x++)
        {
            if (toMorrow.equals (weekDays[6]))
            {
                toMorrow = weekDays[0];
                break;
            }
            else
            {
            if (toMorrow.equals(weekDays[x]))
                {
                    toMorrow = weekDays[x+1];
                    break;

                }
            }
        }


            //loop through the array to match day entered by user
            //then subtract one to return yesterday value           
            for (y=0; y<7; y++)
            {
                if (yesterDay.equals (weekDays[0]))
                    {
                        yesterDay = weekDays[6];
                        break;
                    }
                else
                {
                if (yesterDay.equals    (weekDays[y]))
                    {
                            yesterDay = weekDays[y-1];
                            break;
                    }
                }
            }

            //loop through the array to match value entered by user
            //then use modulus to calculate days in the future
            for (z = 0; z<7; z++)
            {
                if (laterDay.equals (weekDays[z]))
                {
                    d = (farDay + z) % 7;

                    break;

                }
            }

        }

    //define the printDay method to print results
    public void printDay()
    {
        System.out.println("Tomorrow is: " + toMorrow);
        System.out.println("Yesterday was: " + yesterDay);
        System.out.println(e + " days from today is: " + weekDays[d]);
    }

// fin

}
}

1 个答案:

答案 0 :(得分:3)

我不知道在构造对象时你是否拼错了Day

Day theDay = new theDay();

应该是

Day theDay = new Day();