卡在我的最终项目上,要么忘记了如何来回传递数据,要么我只是错过了一些东西。真的很想得到提示或回答谢谢!
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
}
}