如何将一个类中的一个方法的返回值连接到另一个类中的另一个方法?

时间:2022-05-31 23:08:53

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

我正在尝试使类方法playerTurn与另一个类进行交互。

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:

它打印出正确的东西,但我需要它来连接它。如果为true,则第一个是第一个if语句,但它甚至没有运行,而是返回到PlayerTurn方法,即使第二次没有调用它:

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 ("");

}

}

1 个解决方案

#1


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

您忘记在PlayerTurn()方法中为else块添加{}。

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 blockin the rest of your code if you want multiple instructions to be executed together.

如果您希望一起执行多个指令,请确保为if块添加{},并在其余代码中使用block和for loop block。

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.

这里只有System.out.println(“你选择从”+ MysteryCard +“和13”之间的卡片中选择);将在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.

所以这是整个MysteryDeck()方法,我添加了你错过的{}。

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 ("");

}

#1


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

您忘记在PlayerTurn()方法中为else块添加{}。

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 blockin the rest of your code if you want multiple instructions to be executed together.

如果您希望一起执行多个指令,请确保为if块添加{},并在其余代码中使用block和for loop block。

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.

这里只有System.out.println(“你选择从”+ MysteryCard +“和13”之间的卡片中选择);将在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.

所以这是整个MysteryDeck()方法,我添加了你错过的{}。

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 ("");

}