2017-05-26 79 views
1

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

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

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!"); 
+0

如何打印特定的字符串?你将它作为println()方法的参数传递,对吧?那么,你如何做diceCalc应该有权访问diceSum?它不应该将它作为参数,以便调用diceCalc()的diceRoll()可以将它作为参数传递? –

+0

您是否设法解决问题? – luizfzs

回答

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) 
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按值传递的,这意味着如果你改变内部diceRollvarSelect的价值,它不会范围更新select值方法guess。检查这个问题:Is Java “pass-by-reference” or “pass-by-value”?

0

下面是例子:

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

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

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

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++; 
    } 
} 
0

它在你的情况下更好地申报Dice类,然后定义你的方法是骰子

成员

还要定义selectdiceSum作为类的成员变量骰子

之后,它会是简单的在类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!"); 
    } 
}