所以我需要的是:
当用户从Game
类输入数字1时,应在methodOne
类中运行Method
并打印出值。
我在打印值时遇到困难。
如果有人知道该怎么做,请帮忙,我真的很感激。
这是Methods
及其method1
类的代码
import java.util.Scanner;
public class Method {
private static int Str, Dex, Con, Int, Wis, Cha;
static Scanner sc = new Scanner(System.in);
public static Integer[] methodOne() {
List<Integer> stats = new ArrayList<Integer>();
// Entering an integer by the user,
// checking the validity and exiting
// from the game if invalid
System.out.print("Enter Str : ");
Str = sc.nextInt();
while (Str < 0) {
System.err.println("Invalid Input!!");
Str = sc.nextInt();
}
// Entering an integer by the user,
// checking the validity and exiting
// from the game if invalid
System.out.print("Enter Dex : ");
Dex = sc.nextInt();
while (Dex < 0) {
System.err.println("Invalid Input!!");
Dex = sc.nextInt();
}
// Entering an integer by the user,
// checking the validity and exiting
// from the game if invalid
System.out.print("Enter Con : ");
Con = sc.nextInt();
while (Con < 0) {
System.err.println("Invalid Input!!");
Con = sc.nextInt();
}
// Entering an integer by the user,
// checking the validity and exiting
// from the game if invalid
System.out.print("Enter Int : ");
Int = sc.nextInt();
while (Int < 0) {
System.err.println("Invalid Input!!");
Int = sc.nextInt();
}
// Entering an integer by the user,
// checking the validity and exiting
// from the game if invalid
System.out.print("Enter Wis : ");
Wis = sc.nextInt();
while (Wis < 0) {
System.err.println("Invalid Input!!");
Wis = sc.nextInt();
}
// Entering an integer by the user,
// checking the validity and exiting
// from the game if invalid
System.out.print("Enter Cha : ");
Cha = sc.nextInt();
while (Cha < 0) {
System.err.println("Invalid Input!!");
Cha = sc.nextInt();
}
Integer statsValue[] = stats.toArray(new Integer[0]);
return statsValue;
}
}
如您在
Game
中所见,掷出4个骰子,最低的骰子被暂停。我需要将这些累计金额分配给6个变量,并将其存储以备后用。无需打印值。很抱歉上一个有关打印的问题引起的麻烦。
public static int methodTwo() {
int dice1 = (int) (Math.random() * 1000 % 6 + 1);
int dice2 = (int) (Math.random() * 1000 % 6 + 1);
int dice3 = (int) (Math.random() * 1000 % 6 + 1);
int dice4 = (int) (Math.random() * 1000 % 6 + 1);
int total;
// finding the lowest amount
if (dice1 < dice2 && dice1 < dice3 && dice1 < dice4) {
total = dice2 + dice3 + dice4;
} else if (dice2 < dice3 && dice2 < dice4) {
total = dice1 + dice3 + dice4;
} else if (dice3 < dice4) {
total = dice1 + dice2 + dice4;
} else {
total = dice1 + dice2 + dice3;
}
return total;
}
最佳答案
对于Method2
,我建议使用由四个整数组成的数组,代表四个掷骰子。然后,总结一下。使用循环找到最小的纸卷,并从中减去总和。
final int DICES = 4;
int[] diceroll = new int[DICES];
diceroll[0] = (int) (Math.random() * 1000 % 6 + 1);
diceroll[1] = (int) (Math.random() * 1000 % 6 + 1);
diceroll[2] = (int) (Math.random() * 1000 % 6 + 1);
diceroll[3] = (int) (Math.random() * 1000 % 6 + 1);
int total = diceroll[0] + diceroll[1] + diceroll[2] + diceroll[3];
System.out.printf("%d %d %d %d\n", diceroll[0], diceroll[1], diceroll[2], diceroll[3]);
// finding the lowest amount
int min = diceroll[0];
for (int k = 0; k < DICES; k++) {
if (diceroll[k] < min)
min = diceroll[k];
}
total -= min;