Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 能否将方法返回的值作为变量存储在其他方法中?_Java_Scope - Fatal编程技术网

Java 能否将方法返回的值作为变量存储在其他方法中?

Java 能否将方法返回的值作为变量存储在其他方法中?,java,scope,Java,Scope,我是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)

我是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();


}
publicstaticvoidmain(字符串[]args){
//说明:21点计划-单人21点vs经销商
扫描仪s=新的扫描仪(System.in);
System.out.println(“请输入您的姓名:”);
字符串名称=s.nextLine();
System.out.print(“请输入您的余额:$”;
int bal=s.nextInt();
//申报诉讼
最终整数黑桃=0;
最终积分=1;
最终整数钻石=2;
最终积分=3;
//完成了诉讼申报
//申报卡
最终积分1=1;
最终积分11=11;
最终int 2=2;
最终int 3=3;
最终int 4=4;
最终int 5=5;
最终int 6=6;
最终整数7=7;
最终整数8=8;
最终整数9=9;
最终整数十=10;
最终int插孔=10;
最终整数=10;
最终积分王=10;
//申报完毕
//解释规则:
System.out.println(“\n 21点的规则很简单:\n”);
System.out.println(“您和庄家得到两张牌……玩家的两张牌都面朝上,\n而庄家的牌只有一张面朝上,另一张面朝下。”);
System.out.println(“然后一次抽一张牌。每张牌的值在1到11之间。目标是\n尽可能接近21而不超过。”);
System.out.println(“\n值如下所示:”);
System.out.println(“ACE=“+ACE\u 1+”或“+ACE\u 11+”\nTWO=“+TWO+”\ntree=“+THREE+”\nFOUR=“+FOUR+”\nFIVE=“+FOUR+”\nSIX=“+FIVE+”\nSIX=“+SIX+”\nSEVEN=“+SEVEN+”\nEIGHT=“+EIGHT+”\nNINE=“+NINE+”\nTEN=“+TEN+”\nJACK=“+JACK+”\nQUEEN=“+QUEEN+”\nQUEEN=“+QUEEN+”\nKING=“+KING+”\n”);
//为“下注”指定一个金额
System.out.print(“请输入您的赌注:$”;
int bet=s.nextInt();
//检查赌注是否比{user}的钱多。
while(赌注>余额){
println(“很好的尝试!打赌比你有更多的钱,再试一次。(余额=$”+bal+);
System.out.print(“请输入您的赌注:$”;
bet=s.nextInt();
//完成检查下注金额。
}
如果(下注
有没有一种存储值的方法,这样我就可以在我的主类中使用它? 这是一个21点游戏,因此值将是卡的值加在一起的总和。
现在我每次得到一张新的玩家卡的值(玩家值);被称为。有没有办法存储第一张卡的值,这样我就可以在不重置值的情况下添加卡?

您的方法返回
,这样您就可以将其存储在主方法中,如下所示:

int playerCard1 = newPlayerCard(0);