TransWikia.com

How to make an arraylist for the last dice rolls?

Stack Overflow Asked by Acidixs on November 29, 2021

I’ve created a basic dice game. I would like to have a feature where if you choose to no longer play, you will get a list of all your previous rolls. I’ve made a method for generating the 2 random numbers for the dices. I can’t access them since they are in the method.

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class DicegameV2 {

    // method for generating random number for dice
    static void dice_roll() {

        Random rand = new Random();
       int dice1 = rand.nextInt(6) +1;
       int dice2 = rand.nextInt(6) +1;

        System.out.println("you rolled " + dice1 + " and "  +  dice2);
    }
    
    public static void main(String[] args) {


     ArrayList<Integer> last_dice_rolls = new ArrayList<Integer>();

        boolean rollAgain = true;

        // gets username
        Scanner scanner = new Scanner(System.in);
        System.out.println("What's your username?");
        String username = scanner.nextLine();
        System.out.println("Hello, " + username);


        while (rollAgain) {

                System.out.println("Roll the dice?");
                String choice = scanner.nextLine();

                if (choice.equalsIgnoreCase("yes"))
                dice_roll();

                else if (choice.equalsIgnoreCase("no")) {
                    System.out.println("Thanks for playing!nyour last rolls was " + last_dice_rolls);
                    rollAgain = false;

                } else {
                    System.out.println("Please input yes or no");
                }
            }
        }

    }









3 Answers

Make the arrayList last_dice_rolls an an attribute of the class Dicegame v2 so you can access it everywhere in your class.if you declare it in the main method,it will be accessible in the main method only.Try this:

import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class DicegameV2 {
    private static List<Integer> last_dice_rolls;
 
static void dice_roll() {
    if(last_dice_rolls == null)
       last_dice_rolls = new ArrayList<Integer>();

    Random rand = new Random();
   int dice1 = rand.nextInt(6) +1;
   int dice2 = rand.nextInt(6) +1;
   
   last_dice_rolls.add(dice1);
   last_dice_rolls.add(dice2);
    System.out.println("you rolled " + dice1 + " and "  +  dice2);
}

public static void main(String[] args) {

    boolean rollAgain = true;

    // gets username
    Scanner scanner = new Scanner(System.in);
    System.out.println("What's your username?");
    String username = scanner.nextLine();
    System.out.println("Hello, " + username);


    while (rollAgain) {

            System.out.println("Roll the dice?");
            String choice = scanner.nextLine();

            if (choice.equalsIgnoreCase("yes"))
            dice_roll();

            else if (choice.equalsIgnoreCase("no")) {
                System.out.println("Thanks for playing!nyour last rolls was ");
      for(int roll : last_dice_rolls) 
             System.out.println(roll);

                rollAgain = false;

            } else {
                System.out.println("Please input yes or no");
            }
        }
    }

}

java share edit follow flag

Answered by PopJoestar on November 29, 2021

You have the right idea with creating an ArrayList to keep the rolls in, but, as you found, your rolls are in dice1 and dice2 variables in the dice_roll method, so you can't put them into your ArrayList.

You could, as akuzminykh suggested, return the rolls from dice_roll(), but that's a bit tricky as it's two numbers, and a method returns one value, so you'd have to wrap them in something to return them (which is the right thing to do but a bit complex for a first program).

Java is object-oriented, which means that methods belong to objects, which can also keep data. That would be a good way of keeping the non-transient data you're trying to accumulate.

However, your methods are "static", which means they aren't part of any object. main() has to be static, as that's how programs in Java are started.

So you have several steps to make this work:

  1. Have your main() method just create a new DicegameV2 object and call a play() method on it
  2. move the current contents of the main() method into this new play() method, which is not static.
  3. move the ArrayList last_dice_rolls out of the method, so it is not a local variable but an object member
  4. make the dice_roll() method non-static, so it can access the last_dice_rolls member, like last_dice_rolls.add(dice1);
  5. loop over last_dice_rolls and output the values at the end

Your new main() method would look like this (I'm not going to write your whole program)

public static void main(String[] args) {
  DicegameV2 game = new DicegameV2();
  game.play();
}

Answered by Andrew McGuinness on November 29, 2021

you can create a class Rolls

public class Rolls{

 private int idRoll;
 private int dice1;
 private int dice2;

 public void setIdRoll(int id){
  idRoll = id;
 }

 public void setDice1(int d1){
  dice1 = d1;
 }

 public void setDice2(int d2){
  dice2 = d2;
 }

 public String toString(){
   return "Roll number: " + idRoll + " dice1: " + dice1 + " dice2: " + dice2     + "n";
  }
}

and then use it on your main class

import java.util.*;

public class DicegameV2{

// method for generating random number for dice
public static void dice_roll(Rolls rolls) {
    
   Random rand = new Random();
   int dice1 = rand.nextInt(6) +1;
   int dice2 = rand.nextInt(6) +1;
   
   rolls.setDice1(dice1);
   rolls.setDice2(dice2);

   System.out.println("you rolled " + dice1 + " and "  +  dice2);
 }

 public static void main(String[] args) {
  
  //I decided use LinkedList
  List<Rolls> last_dice_rolls = new LinkedList<Rolls>();
  int idRoll = 0;

    boolean rollAgain = true;

    // gets username
    Scanner scanner = new Scanner(System.in);
    System.out.println("What's your username?");
    String username = scanner.nextLine();
    System.out.println("Hello, " + username);


    while (rollAgain) {

            System.out.println("Roll the dice?");
            String choice = scanner.nextLine();

            if (choice.equalsIgnoreCase("yes")){
                Rolls rolls = new Rolls();
                rolls.setIdRoll(idRoll++);
            
                dice_roll(rolls);
                
               last_dice_rolls.add(rolls);
            }
             else if (choice.equalsIgnoreCase("no")) {
                System.out.println("Thanks for playing!nyour last rolls was " + "n");
                for(Rolls r: last_dice_rolls){
                    System.out.println(r);
                }
                rollAgain = false;

            } else {
                System.out.println("Please input yes or no");
            }
        }
    }
}

Answered by Jordy on November 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP