Hello
I cant figure out how to recive a boolean from a method in another class.

    public boolean arYatzy() {
        boolean yatzy = true;

        for (int i = 0; i < tarn.length - 1; i++) {


            yatzy = yatzy && (tarn[i] == tarn[i + 1]);
        }

        if (yatzy) {
            return true;

        }

        else {
            return false;   

        }


    }



 public static void  main(String[] args){
        Yatzy y2 = new Yatzy();

         int[] t2 = new int[5];
            int ggr=0;
            int antal=0;
            System.out.println("loop");
            boolean varv = true; 

            long startTid = System.currentTimeMillis();

            do{



                for(int i=0; i < t2.length; i++)
                {

                    t2[i] = 1 + (int) ((6 - 1 + 1) * Math.random());

                }




                if(y2.arYatzy()==true){
                    antal++;
                }   
                else if(y2.arYatzy()==false){

                }


            ggr++;
            varv = (ggr == 100000);





            } while(!varv);
            long slutTid = System.currentTimeMillis();
            System.out.println("Det blev yatzy "+antal+" gånger"+" Sökning tog :"+(slutTid - startTid) + " ms");
     }

}   

I want to perform the if /else statment in the main class depending on what arYatzy returns.

That's what the code on lines 50-55 does right now. What exactly is your question?

Can the returned value change for each time the method is called? If not, call it once and save what is returned in a boolean variable that can be used in the if statement.
Since a boolean can only have two values, only one test needs be made. If the value of the variable is NOT true, its value must be false. The if test in the else clause is not needed,

                if(y2.arYatzy()){
                    antal++;
                }   
                else{
                  // here for false
                }

Thanks for the answers.
I changed the code and used
antal=y2.ärYatzy()?1:0;

if (antal==1)
etc..

I just had trouble figureing out how to evalute if the return value was true or false. Im just started playing around with java so i guess im gonna make a few more rookie mistakes.

That code is a confusing way to save the value returned by the method. Use a boolean variable:
boolean methodResults = y2.arYatzy(); // call method and save its results

Then you can use that variable in the if statement:

if(methodResults) { 
...
}

Ok thanks. I guess it makes sens to use the same type for readability and save boolean values in boolean varibels. I keep that i mind in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.