java - When I compile I get an error as "ask" cannot be resolved or is not a field" -


package mypackage;   import java.util.random;  interface sharedconstants {     int no=0;     int yes=1;     int maybe=2;     int later=3;     int soon=4;     int never=5; }  class b implements sharedconstants {  random rand=new random();      int ask()     {         int prob=(int) (100 * rand.nextdouble());         if (prob<30)             return no;         else if(prob<60)             return yes;         else if(prob<75)             return later;         else if(prob<90)             return soon;         else             return never;     }  }  class c implements sharedconstants  {     static void answer(int result)      {         switch(result)         {             case no:                 system.out.println("no");                 break;             case yes:                 system.out.println("yes");                 break;             case later:                 system.out.println("later");                 break;             case soon:                 system.out.println("soon");                 break;             case never:                 system.out.println("never");                 break;         }     } }  class interfacedem1  {     public static void main(string[] args)      {         system.out.println("prog starts");         b dew=new b();         answer(dew.ask);         answer(dew.ask);         answer(dew.ask);         answer(dew.ask);         system.out.println("prog ends");      } }    

ask() method, not field. need call method:

answer(dew.ask()); 

also, should replace interface enum. it's better suited storing enumerated constants:

enum probabilities {     no,      yes,      maybe,     later,     soon,     never; } 

and leave class out implementing interface.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -