gpt4 book ai didi

java - 如何将类中的方法调用到 Game 类?

转载 作者:行者123 更新时间:2023-11-30 06:13:03 26 4
gpt4 key购买 nike

所以我需要的是:

  • 当用户输入 Game 中的数字 1 时类它应该运行 methodOneMethod类并打印出值。

我在打印值时遇到困难。如果有人知道如何做到这一点,请帮忙,我真的很感激。

这是 Methods 的代码及其method1Game

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 之前,打印 ArrayList stats .
  • 在游戏中,指定 int[]return Method1 的值,然后使用循环单独打印每个整数。

这是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;
}

关于java - 如何将类中的方法调用到 Game 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49828132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com