您可以将方法的返回值存储在不同方法中作为变量吗?

Can you store a returned value from a method in a different method as a variable?

我是 java 编程新手,我还不了解变量作用域,所以我不确定这是否可行,但是:

我正在尝试存储:

return value;

作为我的主要方法中的变量。

我有一个名为 newPlayerCard() 的方法可以为玩家生成一张扑克牌:

public static int newPlayerCard(int value) {

    Random r = new Random();

    int card = r.nextInt(12) + 1;
    int suit = r.nextInt(3) + 1;

    if (card == 1 && suit == 1) {
        System.out.println("You drew an ACE of CLUBS.");
        value = 11;
    }
    else if (card == 1 && suit == 2) {
        System.out.println("You drew an ACE of DIAMONDS.");
        value = 11;
    }
    else if (card == 1 && suit == 3) {
        System.out.println("You drew an ACE of HEARTS.");
        value = 11;
    }
    else if (card == 1 && suit == 4) {
        System.out.println("You drew an ACE of SPADES.");
        value = 11;
    }
    else if (card == 2 && suit == 1) {
        System.out.println("You drew a TWO of CLUBS.");
        value = 2;
    }
    else if (card == 2 && suit == 2) {
        System.out.println("You drew a TWO of DIAMONDS.");
        value = 2;
    }
    else if (card == 2 && suit == 3) {
        System.out.println("You drew a TWO of HEARTS.");
        value = 2;
    }
    else if (card == 2 && suit == 4) {
        System.out.println("You drew a TWO of SPADES.");
        value = 2;
    }
    else if (card == 3 && suit == 1) {
        System.out.println("You drew a THREE of CLUBS.");
        value = 3;
    }
    else if (card == 3 && suit == 2) {
        System.out.println("You drew a THREE of DIAMONDS.");
        value = 3;
    }
    else if (card == 3 && suit == 3) {
        System.out.println("You drew a THREE of HEARTS.");
        value = 3;
    }
    else if (card == 3 && suit == 4) {
        System.out.println("You drew a THREE of SPADES.");
        value = 3;
    }
    else if (card == 4 && suit == 1) {
        System.out.println("You drew a FOUR of CLUBS.");
        value = 4;
    }
    else if (card == 4 && suit == 2) {
        System.out.println("You drew a FOUR of DIAMONDS.");
        value = 4;
    }
    else if (card == 4 && suit == 3) {
        System.out.println("You drew a FOUR of HEARTS.");
        value = 4;
    }
    else if (card == 4 && suit == 4) {
        System.out.println("You drew a FOUR of SPADES.");
        value = 4;
    }
    else if (card == 5 && suit == 1) {
        System.out.println("You drew a FIVE of CLUBS.");
        value = 5;
    }
    else if (card == 5 && suit == 2) {
        System.out.println("You drew a FIVE of DIAMONDS.");
        value = 5;
    }
    else if (card == 5 && suit == 3) {
        System.out.println("You drew a FIVE of HEARTS.");
        value = 5;
    }
    else if (card == 5 && suit == 4) {
        System.out.println("You drew a FIVE of SPADES.");
        value = 5;
    }
    else if (card == 6 && suit == 1) {
        System.out.println("You drew a SIX of CLUBS.");
        value = 6;
    }
    else if (card == 6 && suit == 2) {
        System.out.println("You drew a SIX of DIAMONDS.");
        value = 6;
    }
    else if (card == 6 && suit == 3) {
        System.out.println("You drew a SIX of HEARTS.");
        value = 6;
    }
    else if (card == 6 && suit == 4) {
        System.out.println("You drew a SIX of SPADES.");
        value = 6;
    }
    else if (card == 7 && suit == 1) {
        System.out.println("You drew a SEVEN of CLUBS.");
        value = 7;
    }
    else if (card == 7 && suit == 2) {
        System.out.println("You drew a SEVEN of DIAMONDS.");
        value = 7;
    }
    else if (card == 7 && suit == 3) {
        System.out.println("You drew a SEVEN of HEARTS.");
        value = 7;
    }
    else if (card == 7 && suit == 4) {
        System.out.println("You drew a SEVEN of SPADES.");
        value = 7;
    }
    else if (card == 8 && suit == 1) {
        System.out.println("You drew a EIGHT of CLUBS.");
        value = 8;
    }
    else if (card == 8 && suit == 2) {
        System.out.println("You drew a EIGHT of DIAMONDS.");
        value = 8;
    }
    else if (card == 8 && suit == 3) {
        System.out.println("You drew a EIGHT of HEARTS.");
        value = 8;
    }
    else if (card == 8 && suit == 4) {
        System.out.println("You drew a EIGHT of SPADES.");
        value = 8;
    }
    else if (card == 9 && suit == 1) {
        System.out.println("You drew a NINE of CLUBS.");
        value = 9;
    }
    else if (card == 9 && suit == 2) {
        System.out.println("You drew a NINE of DIAMONDS.");
        value = 9;
    }
    else if (card == 9 && suit == 3) {
        System.out.println("You drew a NINE of HEARTS.");
        value = 9;
    }
    else if (card == 9 && suit == 4) {
        System.out.println("You drew a NINE of SPADES.");
        value = 9;
    }
    else if (card == 10 && suit == 1) {
        System.out.println("You drew a TEN of CLUBS.");
        value = 10;
    }
    else if (card == 10 && suit == 2) {
        System.out.println("You drew a TEN of DIAMONDS.");
        value = 10;
    }
    else if (card == 10 && suit == 3) {
        System.out.println("You drew a TEN of HEARTS.");
        value = 10;
    }
    else if (card == 10 && suit == 4) {
        System.out.println("You drew a TEN of SPADES.");
        value = 10;
    }
    else if (card == 11 && suit == 1) {
        System.out.println("You drew a JACK of CLUBS.");
        value = 11;
    }
    else if (card == 11 && suit == 2) {
        System.out.println("You drew a JACK of DIAMONDS.");
        value = 11;
    }
    else if (card == 11 && suit == 3) {
        System.out.println("You drew a JACK of HEARTS.");
        value = 11;
    }
    else if (card == 11 && suit == 4) {
        System.out.println("You drew a JACK of SPADES.");
        value = 11;
    }
    else if (card == 12 && suit == 1) {
        System.out.println("You drew a QUEEN of CLUBS.");
        value = 12;
    }
    else if (card == 12 && suit == 2) {
        System.out.println("You drew a QUEEN of DIAMONDS.");
        value = 12;
    }
    else if (card == 12 && suit == 3) {
        System.out.println("You drew a QUEEN of HEARTS.");
        value = 12;
    }
    else if (card == 12 && suit == 4) {
        System.out.println("You drew a QUEEN of SPADES.");
        value = 12;
    }
    else if (card == 13 && suit == 1) {
        System.out.println("You drew a KING of CLUBS.");
        value = 13;
    }
    else if (card == 13 && suit == 2) {
        System.out.println("You drew a KING of DIAMONDS.");
        value = 13;
    }
    else if (card == 13 && suit == 3) {
        System.out.println("You drew a KING of HEARTS.");
        value = 13;
    }
    else if (card == 13 && suit == 4) {
        System.out.println("You drew a KING of SPADES.");
        value = 13;
    }
    else {
        System.out.println("ERROR: THE CARD WAS NOT RECOGNIZED");
        value = 0;
    }

    return value;

}

我这里也有我的主要方法:

public static void main(String[] args) {

    //Description: Blackjack Program - Single player BlackJack vs Dealer

    Scanner s = new Scanner(System.in);

    System.out.println("Please enter your name: ");
    String name = s.nextLine();
    System.out.print("Please enter your balance: $");
    int bal = s.nextInt();

    // Declaring Suits
    final int SPADES = 0;
    final int HEARTS = 1;
    final int DIAMONDS = 2;
    final int CLUBS = 3;
    // Finished declaring suits

    // Declaring cards
    final int ACE_1 = 1;
    final int ACE_11 = 11;
    final int TWO = 2;
    final int THREE = 3;
    final int FOUR = 4;
    final int FIVE = 5;
    final int SIX = 6;
    final int SEVEN = 7;
    final int EIGHT = 8;
    final int NINE = 9;
    final int TEN = 10;
    final int JACK = 10;
    final int QUEEN = 10;
    final int KING = 10;
    // Finished declaring cards

    // Explaining the rules:
    System.out.println("\nThe rules of blackjack are simple: \n");
    System.out.println("You and the dealer are dealt two cards... Both of the player\'s cards are face up,\nwhile only one of the dealer\'s cards is face up, the other one is face down. ");
    System.out.println("You then draw cards one at a time. Each card has a value between 1 and 11. The goal\nis to get as close as possible to 21 without going over.");
    System.out.println("\nThe values are shown below: ");
    System.out.println("ACE = " +  ACE_1 + " or " + ACE_11 + "\nTWO = " + TWO + "\nTHREE = " + THREE + "\nFOUR = " + FOUR + "\nFIVE = " + FIVE + "\nSIX = " + SIX + "\nSEVEN = " + SEVEN + "\nEIGHT = " + EIGHT + "\nNINE = " + NINE + "\nTEN = " + TEN + "\nJACK = " + JACK + "\nQUEEN = " + QUEEN + "\nKING = " + KING + "\n");

    // Assigns an amount to "bet"
    System.out.print("Please enter your bet: $");
    int bet = s.nextInt();

    // Checks to see if the bet is more money than {user} has.
    while (bet > bal) {
        System.out.println("Nice try! That bet is more money than you have, try again. (Balance = $" + bal + ")");
        System.out.print("Please enter your bet: $");
        bet = s.nextInt();
        // Finishes checking the bet amount.
    }

    if (bet < bal) {
        bal -= bet;
        System.out.println("Your bet of $" + bet + " has been subracted from your account. Your new balance is $" + bal + ".");
    }


    // Creates two cards for the player
    int playerValue = 0;
    newPlayerCard(playerValue);
    newPlayerCard(playerValue);
    // Creates one card for the dealer
    newDealerCard();


}

有没有办法存储价值,以便我可以在我的主要 class 中使用它? 这是一个 21 点游戏,因此值将是纸牌值加在一起的总和。 现在我每次得到一张牌的价值 newPlayerCard(playerValue);叫做。有没有办法存储第一张卡的值,以便我可以在不重置值的情况下添加卡?

你方法 returns value 所以你可以像这样将它存储在你的主要方法中:

int playerCard1 = newPlayerCard(0);