DDSasikala
DDSasikala

Reputation: 45

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

So what I need is:

I'm having difficulties in printing the values. If someone know how to do this please help, I really appreciate it.

Here is the code for Methods with its method1 and the 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;
    }
}

As you can see in methodTwo, 4 dice are rolled and the lowest value is suspended.

I need to assign these rolled amounts to the 6 variables and store them for later use. No need to print the values. Sorry for the trouble caused in the previous question about printing.

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

Upvotes: 1

Views: 85

Answers (2)

For Method2, I would suggest using an array of four integers, representing the four dice rolls. Then, sum it up. Find the smallest roll using a loop and subtract the sum from it.

    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;

Upvotes: 0

Unfortunately, there are some important issues with your code, that renders your ArrayList empty, meaning that the results can't be correctly printed as expected.

  • You need to add the six inputted variables to stats.
  • Are you sure you will only input six variables? If so, an ArrayList is not required, you can just use int[6] statsValue.
  • List<Integer> stats = new ArrayList<Integer>();, that should be ArrayList<Integer>

You forgot to print the Integer ArrayList converted to array. To do that, you can either:

  • Before returning StatsValue, print ArrayList stats.
  • In the game, assign an int[] to the return value of Method1, then use a loop to print each integer individually.

Here is the revised code for Integer[] method1 assuming you still use the 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;
}

Upvotes: 1

Related Questions