我试图在方法之间传递变量,但我不知道在这个例子中如何做。 PS我是java的新手
public int calcTotalPoints() {
int sumOfDice = val1 + val2 + val3;
int total=0;
if (threeSame()){
total= sumOfDice + 10;
}
else if (pair()){
total= sumOfDice + 20;
}
else if (allDifferent()){
total= sumOfDice;
}
return total;
}
public void printResult() {
System.out.println("points: " + total);
}
我正在尝试使用其他方法打印总计
答案 0 :(得分:0)
如果我正确理解您的问题,请执行以下操作:
int val1 = 10;
int val2 = 5;
int val3= 2;
//you call method as follows
int total = calcTotalPoints( val1, val2,val3 );
//method variables declaration as follows
public int calcTotalPoints(int val1, int val2, int val3)
{
int sumOfDice = val1 + val2 + val3;
int total=0;
if (threeSame()){
total= sumOfDice + 10;
}
else if (pair()){
total= sumOfDice + 20;
}
else if (allDifferent()){
total= sumOfDice;
}
return total;
}
答案 1 :(得分:0)
由于您的calcTotalPoint()
已经返回整数,因此您可以执行System.out.println("points: " + calcTotalPoints())