如何将 class 中的方法调用到游戏 class 中?
How can I call the methods in a class to a Game class?
所以我需要的是:
- 当用户从
Game
class 输入数字 1 时,它应该 运行 methodOne
在 Method
class 并打印输出值。
我在打印这些值时遇到困难。
如果有人知道如何做到这一点,请帮助,我真的很感激。
这是 Methods
的代码及其 method1
和 Game
class
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;
}
}
正如您在methodTwo
中看到的那样,掷了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;
}
不幸的是,您的代码存在一些重要问题,这些问题使您的 ArrayList 为空,这意味着无法按预期正确打印结果。
- 您需要将输入的六个变量添加到
stats
。
- 您确定只输入六个变量吗?如果是这样,则不需要 ArrayList,您可以使用
int[6] statsValue
.
List<Integer> stats = new ArrayList<Integer>();
,应该是ArrayList<Integer>
您忘记打印转换为数组的 Integer ArrayList。为此,您可以:
- 在返回 StatsValue 之前,print ArrayList
stats
.
- 在游戏中,将
int[]
分配给Method1的return
值,然后使用循环分别打印每个整数。
这里是 Integer[] method1
的修订代码,假设您仍然使用 ArrayList。
public static Integer[] methodOne() {
ArrayList<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();
}
stats.add(Str); // Add inputs to ArrayList stats
System.out.print("Enter Dex : ");
Dex = sc.nextInt();
while (Dex < 0) {
System.err.println("Invalid Input!!");
Dex = sc.nextInt();
}
System.out.print("Enter Con : ");
stats.add(Dex);
Con = sc.nextInt();
while (Con < 0) {
System.err.println("Invalid Input!!");
Con = sc.nextInt();
}
System.out.print("Enter Int : ");
stats.add(Con);
Int = sc.nextInt();
while (Int < 0) {
System.err.println("Invalid Input!!");
Int = sc.nextInt();
}
stats.add(Int);
System.out.print("Enter Wis : ");
Wis = sc.nextInt();
while (Wis < 0) {
System.err.println("Invalid Input!!");
Wis = sc.nextInt();
}
stats.add(Wis);
System.out.print("Enter Cha : ");
Cha = sc.nextInt();
while (Cha < 0) {
System.err.println("Invalid Input!!");
Cha = sc.nextInt();
}
stats.add(Cha);
System.out.println(stats); // You can print elements of an Arraylist directly
Integer statsValue[] = stats.toArray(new Integer[0]);
return statsValue;
}
对于Method2
,我建议使用一个包含四个整数的数组,代表四个骰子。然后,总结一下。使用循环找到最小的 roll 并从中减去总和。
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;
所以我需要的是:
- 当用户从
Game
class 输入数字 1 时,它应该 运行methodOne
在Method
class 并打印输出值。
我在打印这些值时遇到困难。 如果有人知道如何做到这一点,请帮助,我真的很感激。
这是 Methods
的代码及其 method1
和 Game
class
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;
}
}
正如您在methodTwo
中看到的那样,掷了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;
}
不幸的是,您的代码存在一些重要问题,这些问题使您的 ArrayList 为空,这意味着无法按预期正确打印结果。
- 您需要将输入的六个变量添加到
stats
。 - 您确定只输入六个变量吗?如果是这样,则不需要 ArrayList,您可以使用
int[6] statsValue
. List<Integer> stats = new ArrayList<Integer>();
,应该是ArrayList<Integer>
您忘记打印转换为数组的 Integer ArrayList。为此,您可以:
- 在返回 StatsValue 之前,print ArrayList
stats
. - 在游戏中,将
int[]
分配给Method1的return
值,然后使用循环分别打印每个整数。
这里是 Integer[] method1
的修订代码,假设您仍然使用 ArrayList。
public static Integer[] methodOne() {
ArrayList<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();
}
stats.add(Str); // Add inputs to ArrayList stats
System.out.print("Enter Dex : ");
Dex = sc.nextInt();
while (Dex < 0) {
System.err.println("Invalid Input!!");
Dex = sc.nextInt();
}
System.out.print("Enter Con : ");
stats.add(Dex);
Con = sc.nextInt();
while (Con < 0) {
System.err.println("Invalid Input!!");
Con = sc.nextInt();
}
System.out.print("Enter Int : ");
stats.add(Con);
Int = sc.nextInt();
while (Int < 0) {
System.err.println("Invalid Input!!");
Int = sc.nextInt();
}
stats.add(Int);
System.out.print("Enter Wis : ");
Wis = sc.nextInt();
while (Wis < 0) {
System.err.println("Invalid Input!!");
Wis = sc.nextInt();
}
stats.add(Wis);
System.out.print("Enter Cha : ");
Cha = sc.nextInt();
while (Cha < 0) {
System.err.println("Invalid Input!!");
Cha = sc.nextInt();
}
stats.add(Cha);
System.out.println(stats); // You can print elements of an Arraylist directly
Integer statsValue[] = stats.toArray(new Integer[0]);
return statsValue;
}
对于Method2
,我建议使用一个包含四个整数的数组,代表四个骰子。然后,总结一下。使用循环找到最小的 roll 并从中减去总和。
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;