*有两个村,张村和李村 张村的人星期一三五说谎 李村的人星期二四六说谎
*
* 一人问,今天周几?结果两个村的人都回答“前天是我说谎的日子”
*
* 问:今天是周几?最近看到也在解这个题,面向过程的解法基本是熟悉了,正在尝试用面向对象来解决这个问题,但在构建村子类的时候卡了思路了,大家也尝试解解看,有没有什么好的思路一起共享。下面是我的代码块,只是运行的结果是周日,不知道错在哪了,求大神帮忙查查
public class WhenLie {
        public static void main(String[] args) {
                Day day;
                VillageA a;
                VillageB b;                //对每一天进行判断,看前天是否都为说谎日
                for (int i = 1; i <= 7; i++) {
                        day = new Day(i);
                        a = new VillageA(day);
                        b = new VillageB(day);
                        Day beforeYestoday = day.yestaday().yestaday();
                        boolean a_answer = a.answer(beforeYestoday);
                        boolean b_answer = b.answer(beforeYestoday);
                       
                        if (!a_answer && !b_answer) {
                                System.out.println(i);
                        }
                }
        }
}//A村构建一个类
class VillageA {
        boolean isTellLie = true;    //true代表说真话
       
        //初始化判定当天是否说谎日
        VillageA(Day day) {
                if (day.id == 1 || day.id == 3 || day.id == 5) {
                        isTellLie = false;
                }
        }        //回答某一天是否说谎日,此day不一定是当天
        boolean answer(Day day) {
                VillageA v = new VillageA(day);
                if (isTellLie) {
                        return v.isTellLie;
                } else {
                        return !v.isTellLie;
                }
        }
}class VillageB {
        private boolean isTellLie = true;        VillageB(Day day) {
                if (day.id == 2 || day.id == 4 || day.id == 6) {
                        isTellLie = false;
                }
        }        boolean answer(Day day) {
                VillageA v = new VillageA(day);
                if (isTellLie) {
                        return v.isTellLie;
                } else {
                        return !v.isTellLie;
                }
        }
}//构建一个日期类,有成员变量id表明是星期几(1代表周一,2代表周二,类推)
class Day {        int id;        Day(int id) {
                this.id = id;
        }        //返回一个昨天
        Day yestaday() {
                if (id > 1) {
                        id--;
                } else if (id == 1) {
                        id = 7;
                }                return this;
        }
}

解决方案 »

  1.   

    看了一下是不是这些错了
    class VillageB {
            private boolean isTellLie = true;        VillageB(Day day) {
                    if (day.id == 2 || day.id == 4 || day.id == 6) {
                            isTellLie = false;
                    }
            }        boolean answer(Day day) {
                    VillageB v = new VillageB(day);
                    if (isTellLie) {
                            return v.isTellLie;
                    } else {
                            return !v.isTellLie;
                    }
            }
    }
      

  2.   


                    if (isTellLie) {
                             return v.isTellLie;
                     } else {
                             return !v.isTellLie;
                     }//这个判断LZ是怎么理解的我想这样写成这样: 
    return !(v.isTellLie&&this.isTellLie);
    在main函数里判断: 
    if (a_answer && b_answer) {
         System.out.println(i);
     }
      

  3.   


    nice,现在对了,郁闷,这些小毛病我什么时候才可以改过来啊!!!谢谢了!你的那个写法恐怕不行,return !(v.isTellLie&&this.isTellLie);若如此的话,如果当天是说谎日(也就是this.isTellLie=false),那么不论判断哪一天(v.isTellLie),返回的值都是true,这达不到判断的目的。
      

  4.   


    主要是用面向对象的思维,构建出三个类,(其实是两种类),村子类和日期类。
    村子类的属性有:boolean isTellLie  //今天是否说谎
            方法:  boolean answer(Day day)     //判断某一天是否为说谎日(当然这里的判断是结合了当日是否是说谎日判断的日期类属性   int id    //今天是周几
          方法   Day yestoday()   //返回一个昨天这样就可以循环每一天去问两个村子的人前天是否说谎,得出回答前天都是说谎的那一天就是了。就这样了,希望你能明白。
      

  5.   

    我是这样想的,就是说判断张村和李村当天和前天逻辑能否正确,如果都正确,那么就是输出。
    因为张村 135说谎,李村246说谎,那么如何当天两村都说谎是不可能的,如果两村都说真话,那就是7,推之,应该是前天两村都说谎,也是不可能的。所以只能是两村当天和前天&&运算的反运算为真时,才成立。

    F T
    T F

    T F
    F T
    情况的遍历判断。
    我这个思路对不
      

  6.   

    //enum weekday{sunday,monday,}
    class Day
    {
    int d;
     Day(int d);
     Day operator -(int n)
     {
      
      d+=n;
      d%=7; 
      return *this;
     } 
    };
    class
    Vellege
    {
       Vellege(int v):no(v)
       {
          for(i=0;i<7;i++)
             LieOnoDays[i]=true;
          if(v==0)for(i=1;i<=5;i+=2)
             LieOnoDays[i]=false;
          else for(i=2;i<=6;i+=2)
             LieOnoDays[i]=false;  
                
       } 
       int no;
       bool LieDays[7];
       bool Lie(const Day &d)
       { 
          return LieOnoDays[d.d];   
       }
       public void PrintWeekDay()
      {
          System.out.println(d); 
      }         
     };
    public class WhenLie {         Day DayByAnswer(const Village &a,const Villiage b)
    {   Day today,lastday;
        for(i=0;i<7;i++)
        {
           today.d=i;
           lastday=today-2; 
           if(  
              ( a.Lie(today) && a.Lie(lastday)
               && (!b.Lie(today)) &&(!b.Lie(lastday)) )
              ||( b.Lie(today) && b.Lie(lastday)    
                && (! a.Lie(today)) && (!a.Lie(lastday)) )
               )
           return today;          
        }    

       public static void main(String[] args) 
    {                 Day today; 
                      Village a(0);                 
                      VillageB b(1);  
                                   
           today = DayByAnswer(a,b);
           today.PrintWeekDay();                                                     
                    
           }
    }    
                       
      

  7.   

    注意前天,不是昨天 日期要减2,一共涉及今天,前天两天。
    星期天都不说谎,其他轮流说谎。
    抱歉条件错了,有点绕人。
    //enum weekday{sunday,monday,}
    class Day
    {
    int d;
     Day(int d);
     Day operator -(int n)
     {
      
      d+=n;
      d%=7; 
      return *this;
     } 
    };
    class
    Vellege
    {
       Vellege(int v):no(v)
       {
          for(i=0;i<7;i++)
             LieOnoDays[i]=false;//不说谎      if(v==0)for(i=1;i<=5;i+=2)
             LieOnoDays[i]=true;//说谎
          else for(i=2;i<=6;i+=2)
             LieOnoDays[i]=true;//说谎  
                
       } 
       int no;
       bool LieDays[7];
       bool Lie(const Day &d)
       { 
          return LieOnoDays[d.d];   
       }
       public void PrintWeekDay()
      {
          System.out.println(d); 
      }         
     };
    public class WhenLie {         Day DayByAnswer(const Village &a,const Villiage b)
    {   Day today,lastday;
        for(i=0;i<7;i++)
        {
           today.d=i;
           lastday=today-2; 
           if(  
              ( a.Lie(today) && (!a.Lie(lastday))        //a 今谎,前不
               && (b.Lie(today)) &&(!b.Lie(lastday)) )   //b 今谎,前不
              ||
               ( !a.Lie(today) && (a.Lie(lastday))      //a 今不,前谎
               && (!b.Lie(today)) &&(b.Lie(lastday)) )  //b 今不,前谎
              ||
              ( !a.Lie(today) && (a.Lie(lastday))       //a 今不,前谎  
               && (b.Lie(today)) &&(!b.Lie(lastday)) )  //b 今谎,前不
              ||
               ( a.Lie(today) && (!a.Lie(lastday))      //a 今谎,前不    
               && (!b.Lie(today)) &&(b.Lie(lastday)) )  //a 今不,前谎 
           )
           return today;          
        }    

       public static void main(String[] args) 
    {                 Day today; 
                      Village a(0);                 
                      VillageB b(1);  
                                   
           today = DayByAnswer(a,b);
           today.PrintWeekDay();                                                     
                    
           }
    }  
      

  8.   

    抱歉注释错误
    ( a.Lie(today) && (!a.Lie(lastday))        //a 今谎,前不
               && (b.Lie(today)) &&(!b.Lie(lastday)) )   //b 今谎,前不
              ||
               ( !a.Lie(today) && (a.Lie(lastday))      //a 今不,前谎
               && (!b.Lie(today)) &&(b.Lie(lastday)) )  //b 今不,前谎
              ||
              ( !a.Lie(today) && (a.Lie(lastday))       //a 今不,前谎  
               && (b.Lie(today)) &&(!b.Lie(lastday)) )  //b 今谎,前不
              ||
               ( a.Lie(today) && (!a.Lie(lastday))      //a 今谎,前不    
               && (!b.Lie(today)) &&(b.Lie(lastday)) )   //b  今不,前谎