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

时间:2018-04-14 05:05:48

标签: java methods system.out

所以我需要的是:

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

我在打印价值方面遇到了困难。 如果有人知道怎么做,请帮忙,我真的很感激。

以下是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;
    }
}

正如你在方法中看到的那样,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;
}

2 个答案:

答案 0 :(得分:1)

不幸的是,您的代码存在一些重要问题,导致您的ArrayList 为空,这意味着无法按预期正确打印结果。

  • 您需要将六个输入的变量添加到stats
  • 您确定只输入六个变量吗?如果是这样,则不需要ArrayList,您只需使用int[6] statsValue
  • List<Integer> stats = new ArrayList<Integer>();,应为ArrayList<Integer>

您忘记打印转换为数组的Integer ArrayList。为此,您可以:

  • 在返回StatsValue之前,打印 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;
}

答案 1 :(得分:0)

对于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;