簡體   English   中英

如何將類中的方法調用到 Game 類?

[英]How can I call the methods in a class to a Game class?

所以我需要的是:

  • 當用戶從Game類輸入數字 1 時,它應該運行Method類中的methodOne並打印出值。

我在打印值時遇到困難。 如果有人知道如何做到這一點,請幫忙,我真的很感激。

下面是代碼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 個骰子被methodTwo ,最低的值被暫停。

我需要將這些滾動金額分配給 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>

您忘記打印轉換為數組的整數 ArrayList。 為此,您可以:

  • 在返回 StatsValue 之前,打印ArrayList stats
  • 游戲中,給Method1的return值賦一個int[] ,然后用一個循環來分別打印每個整數。

這是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 ,我建議使用一個由四個整數組成的數組,代表四個骰子。 然后,總結一下。 使用循環找到最小的滾動並從中減去總和。

    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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM