简体   繁体   中英

How do I connect the return from one method in one class, to another method in another class?

I'm trying to make the class method playerTurn interact with another class.

import java.util.*;

public boolean PlayerTurn(){

    System.out.println("To determine who goes first, I am thinking of a number between 1 and 5, including 1 and 5.");
    int comp1 = rand.nextInt(5)+1;
    System.out.println(player1+", input guess: ");
    int g1 = scan.nextInt();
    System.out.println(player2+", input guess: ");
    int g2 = scan.nextInt();

    int p1 = Math.abs (comp1 -g1);
    int p2 = Math.abs(comp1-g2);
if(p1<p2){

    System.out.println("The number was "+comp1+". " +player1+", will go first.");

    return true ;



}
else 
    System.out.println("The number was "+comp1+". "+player2+", will go first.");
    return true;


}

It prints out the right stuff but I need it to connect with this. If true than player1 goes first and thats the first if statement but it doesn't even run and instead goes back to the PlayerTurn method even if it wasn't called a second time:

public class fullCard {
     ArrayList<Integer>cardDraw1 = new ArrayList<Integer>(12);
     ArrayList<Integer>cardDraw2 = new ArrayList<Integer>(12);
     Random rand = new Random();
    Scanner scan = new Scanner(System.in);
    Intro test = new Intro();
    Player p = new Player();

int MysteryCard, ans1, ans2;
int total1, total2, newHighCard, newLowCard;

public fullCard(){

    int total1= 0, total2= 0;
    int newHighCard=0;
    int newLowCard=0;
    MysteryCard = rand.nextInt(13)+1;
}


public String MysteryDeck(){

for(int i =0; i<12; i++){   
    System.out.println("Why");<--this prints
    if(p.PlayerTurn())//DOES NOT WORK AT ALL
        System.out.print(" Player 1   Type 1 to pick from a deck higher than the MysteryCard and Type 2 to draw from a deck lower.");
        ans1 = scan.nextInt();
        if(ans1 ==1)
            System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13");
            for(int k= 0; k < cardDraw1.size(); k++)
                newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
                cardDraw1.add(newHighCard);
            for(int v=0; v<cardDraw1.size(); v++)
                total1+=cardDraw1.get(v);
                System.out.println(total1);

        if(ans1== 2)
            System.out.println("You will draw from a deck 1 to "+MysteryCard);
            for(int k= 0; k<cardDraw1.size();k++)
                newLowCard = rand.nextInt(MysteryCard+1)+1;
                cardDraw1.add(newLowCard);

            for (int v=0; v<cardDraw1.size(); v++)
                total1+=cardDraw1.get(v);
        System.out.println(total1);



    if(!p.PlayerTurn())
        System.out.println("Player 2  Type 1 to pick from a deck higher than the MysterCard and type 2 to draw from a deck lower than the MysteryCard.");
        ans2=scan.nextInt();    
        if(ans2 ==1)
            System.out.println("You have chosen to pick from cards between "+MysteryCard+" and 13");
            for(int k= 0; k<cardDraw2.size(); k++)
                newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
                cardDraw2.add(newHighCard);

            for(int d = 0; d<cardDraw2.size(); d++)
                total2 +=cardDraw2.get(d);

                System.out.println(total2);
        if(ans2== 2)
            System.out.println("You will draw from a deck 1 to "+MysteryCard);
            for(int k= 0; k<cardDraw2.size(); k++)
                newLowCard = rand.nextInt(MysteryCard+1)+1;
                cardDraw2.add(newLowCard);
                System.out.println(cardDraw2);
}   

return ("");

}

}

You forgot to add {} for the else block in PlayerTurn() method.

else
{ // add this
    System.out.println("The number was "+comp1+". "+player2+", will go first.");
    return true;
} // and this

And make sure to add {} for if block and else block and for loop block in the rest of your code if you want multiple instructions to be executed together.

For instance, let's get this piece of your code

if(ans1 ==1)
            System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13");
            for(int k= 0; k < cardDraw1.size(); k++)
                newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
                cardDraw1.add(newHighCard);
            for(int v=0; v<cardDraw1.size(); v++)
                total1+=cardDraw1.get(v);
                System.out.println(total1);

Here only System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13"); will be executed whenever ans1 ==1 .

I think you want to do it as follow (add {} )

 if(ans1 ==1)
 { //add this
                System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13");
                for(int k= 0; k < cardDraw1.size(); k++)
                    newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
                    cardDraw1.add(newHighCard);
                for(int v=0; v<cardDraw1.size(); v++)
                    total1+=cardDraw1.get(v);
                    System.out.println(total1);
}// ans this

So this is the whole MysteryDeck() method where i have added the {} you missed.

public String MysteryDeck(){

for(int i =0; i<12; i++){   
    System.out.println("Why");<--this prints
    if(p.PlayerTurn())
    { // here
        System.out.print(" Player 1   Type 1 to pick from a deck higher than the MysteryCard and Type 2 to draw from a deck lower.");
        ans1 = scan.nextInt();
        if(ans1 ==1)
        { // here
            System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13");
            for(int k= 0; k < cardDraw1.size(); k++)
            { // here
                newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
                cardDraw1.add(newHighCard);
            } // here
            for(int v=0; v<cardDraw1.size(); v++)
            {// here optional
                total1+=cardDraw1.get(v);
            } //here optional
                System.out.println(total1);
        } // here


        if(ans1== 2)
        { // here
            System.out.println("You will draw from a deck 1 to "+MysteryCard);
            for(int k= 0; k<cardDraw1.size();k++)
            { // here
                newLowCard = rand.nextInt(MysteryCard+1)+1;
                cardDraw1.add(newLowCard);
            } // here
            for (int v=0; v<cardDraw1.size(); v++)
            { // here optional
                total1+=cardDraw1.get(v);
            } // here optional
            System.out.println(total1);
        } // here

    } // here

    if(!p.PlayerTurn())
    { // here
        System.out.println("Player 2  Type 1 to pick from a deck higher than the MysterCard and type 2 to draw from a deck lower than the MysteryCard.");
        ans2=scan.nextInt();    
        if(ans2 ==1)
        { // here
            System.out.println("You have chosen to pick from cards between "+MysteryCard+" and 13");
            for(int k= 0; k<cardDraw2.size(); k++)
            { // here
                newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1);
                cardDraw2.add(newHighCard);
            } // here

            for(int d = 0; d<cardDraw2.size(); d++)
            { // here optional
                total2 +=cardDraw2.get(d);
            } // here optional

                System.out.println(total2);
        } // here
        if(ans2== 2)
        { // here
            System.out.println("You will draw from a deck 1 to "+MysteryCard);
            for(int k= 0; k<cardDraw2.size(); k++)
            { // here
                newLowCard = rand.nextInt(MysteryCard+1)+1;
                cardDraw2.add(newLowCard);
            } // here
                System.out.println(cardDraw2);
        }//here
    }// here
}   

return ("");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM