在方法

时间:2017-05-26 16:51:50

标签: java methods

我正在制作一个有不同方法的骰子游戏。但是,我似乎无法弄清楚如何在我的方法之间传递变量以便在另一个方法中使用。它从菜单开始,用户将选择"选择" 1为奇数或2为偶然然后转到滚动方法得到滚动的总和然后计算方法告诉用户他们是否有一个或损失。但是,我似乎无法弄清楚如何传递diceSum并在我的方法之间进行选择以便在计算方法中使用。

我似乎无法弄清楚如何在diceCalc()中使用select和diceSum来打印出用户的答案。

private static void guess(){
    Scanner input = new Scanner(System.in);
    System.out.print("(1) Odd\n"
            +"(2) Even\n"
            +"(3) Quit Game\n"
            +"What is your selection: ");
    try {
        int select = input.nextInt();
        if(select == 1) {
            System.out.println("");
            diceRoll();
        } else if(select == 2) {
            System.out.println("");
            diceRoll();
        } else if(select == 3) {
            System.out.println("Thank you for playing Dueling Dice v1.0...");
    system.out(0);
}

private static void diceRoll() {
    int roll1, roll2, diceSum;
    roll1 = (int)(Math.random()*6+1);
    roll2 = (int)(Math.random()*6+1);
    diceSum = roll1 + roll2;

    System.out.println("    Dice 1 roll = " + roll1);
    System.out.println("    Dice 2 roll = " + roll2);
    System.out.println("TOTAL dice roll = " + diceSum);
    diceCalc(); // this is called in the main  after the guess since guess performs the roll 
}

private static void diceCalc() {
    if (diceSum % 2 != 0 && select == 1 || diceSum % 2 == 0 && select == 2) {
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("CONGRATULATIONS! You WIN!!");
        won++;
        played++;
    }else if (diceSum % 2 == 0 && select == 1 || diceSum % 2 != 0 && select == 2){
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("I am sorry you lost!");
        lost++;
        played++;
    }
    System.out.print("it is making it here!");

6 个答案:

答案 0 :(得分:1)

您可以将diceSum传递给diceCalc方法。

diceCalc(diceSum);  // this is called in the main  after the guess since guess performs the roll 

然后将参数添加到diceCalc函数。

private static void diceCalc(int diceSum)

答案 1 :(得分:0)

您可以在Java中使用一些函数和参数。 Check here

尽管如此,你可以这样做:

select参数传递给diceRoll方法

private static void guess(){
  int select = <someValue>
  ...
  diceRoll(select);
  ...
}

diceRoll方法中,您声明如下参数。

private static void diceRoll(int varSelect) {
  ...
  System.out.println("Select value: " + varSelect);
  ...
}

编辑1 :请记住,Java中的原始类型是按值传递的,这意味着如果更改varSelectdiceRoll的值,则不会更新select方法范围内guess的值。请检查此问题:Is Java “pass-by-reference” or “pass-by-value”?

答案 2 :(得分:0)

以下是示例:

void methodA (){
int toB = 5;
methodB(toB);
}
void methodB(int fromA){
 int recievedFromA = fromA;
System.out.println(recievedFromA);
}

您使用的是哪本书或课程? 我想知道你在学习最基本的编程概念之前先来编写一些控制台游戏。如果你是自学者,那么我建议你找一些好的Java书籍,如Joshua Bloch的Effective Java,以帮助你学习你需要学习的最基本和最先进的概念。

答案 3 :(得分:0)

在方法中声明的变量(例如diceSum)仅在特定方法的本地。其他方法无法访问它们。

public static void main(String[] args) {
    int diceSum; // Can only be used in this method
}

public static void diceCalc() {
    // The variable diceSum is not visible here, it can only be accessed
    // by the main() method
}

但是,您可以将参数传递给方法。你只需将它们放在括号之间:

public static void main(String[] args) {
    int diceSum = 3;
    diceCalc(diceSum);
}

public static void diceCalc(int diceSum) {
    // diceSum will contain the value 3
    ...
}

让事物变得更有活力是件好事。例如,diceCalc()方法取决于菜单项编号是奇数还是偶数。如果您想要更改菜单项(或构建图形用户界面,用户单击按钮以在奇数或偶数之间进行选择),您还必须更改diceCalc()方法。

答案 4 :(得分:0)

感谢您的帮助,但想出来了。我知道我必须将我的方法改为int才能返回一个参数并且它完美地工作。

    private static int diceRoll() {
    int roll1, roll2, diceSum;
    roll1 = (int)(Math.random()*6+1);
    roll2 = (int)(Math.random()*6+1);
    diceSum = roll1 + roll2;

    System.out.println("    Dice 1 roll = " + roll1);
    System.out.println("    Dice 2 roll = " + roll2);
    System.out.println("TOTAL dice roll = " + diceSum);
    return diceSum;

}

private static void diceCalc() {
    if (diceSum % 2 != 0 && select == 1 || diceSum % 2 == 0 && select == 2) {
        if (select == 1) {
            System.out.println("\nDice rolled = " + diceSum + " and you selected odd");
        } else {
            System.out.println("\nDice rolled = " + diceSum + " and you selected even");
        }
        System.out.println("CONGRATULATIONS! You WIN!!");
        won++;
        played++;
    }else if (diceSum % 2 == 0 && select == 1 || diceSum % 2 != 0 && select == 2){
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("I am sorry you lost!");
        lost++;
        played++;
    }
}

答案 5 :(得分:0)

在您的情况下,宣布课程Dice然后将您的方法定义为骰子成员

会更好

还将selectdiceSum定义为Dice类的成员变量

之后,在Dice类的任何成员方法中访问这些变量都很简单

通过这种方式,您的程序将更有条理,易于阅读。

public class Dice {

    private int select;
    private int diceSum;

    private void guess(){
    Scanner input = new Scanner(System.in);
    System.out.print("(1) Odd\n"
            +"(2) Even\n"
            +"(3) Quit Game\n"
            +"What is your selection: ");

        select = input.nextInt();
        if(select == 1) {
            System.out.println("");
            diceRoll();
        } else if(select == 2) {
            System.out.println("");
            diceRoll();
        } else if(select == 3) {
            System.out.println("Thank you for playing Dueling Dice v1.0...");
        }
   }
    private void diceRoll() {
    int roll1, roll2;
    roll1 = (int)(Math.random()*6+1);
    roll2 = (int)(Math.random()*6+1);
    diceSum = roll1 + roll2;

    System.out.println("    Dice 1 roll = " + roll1);
    System.out.println("    Dice 2 roll = " + roll2);
    System.out.println("TOTAL dice roll = " + diceSum);
    diceCalc(); // this is called in the main  after the guess since guess performs the roll 
    }

    private void diceCalc() {
    if (diceSum % 2 != 0 && select == 1 || diceSum % 2 == 0 && select == 2) {
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("CONGRATULATIONS! You WIN!!");
        won++;
        played++;
    }else if (diceSum % 2 == 0 && select == 1 || diceSum % 2 != 0 && select == 2){
        if (select == 1) {
            System.out.println("Dice rolled = " + diceSum + "and you selected odd");
        } else {
            System.out.println("Dice rolled = " + diceSum + "and you selected even");
        }
        System.out.println("I am sorry you lost!");
        lost++;
        played++;
    }
    System.out.print("it is making it here!");
    }
}