2012-10-29 21 views
1

卡在我的最终项目中,或者忘记了如何来回传递数据,或者我只是错过了一些东西。真的会喜欢提示或回答谢谢!在main和method之间传递信息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 

} 
} 
+3

什么方法 - 我只看到一种主要方法,没有其他方法?请澄清。请告诉我们你想要做什么,并告诉你如何卡住的细节。 –

+0

对不起,正在粘贴 – user1781758

+2

什么是错误? – case1352

回答

3

我不知道你是否拼写错误Day而构造对象:

Day theDay = new theDay(); 

应该

Day theDay = new Day();