Search code examples
javaarraylistdice

Checking if values stored in an ArrayList are greater than a number


I'm attempting my first project in Java after learning the basics, so I apologize for what is likely a complete beginner question. I have been searching all afternoon for assistance, and my code is starting to look messy and broken.

I am attempting to create a dice roller that rolls a number of d10s, then proceeds to check how many are 7's or above and count/display them as successes. If none of the rolls are a success, I want it to display "Botch".

After some research, I found creating an ArrayList and going from there was (what seems to me as) the best option. But I've been stuck for some time now, with different errors and issues coming up, and each time I take one down, the code gets more confusing and my goal seems farther away. My buddy said "welcome to programming" and suggested I ask the community.

Here is what I'm working with:

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

public class RollDie {
    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        String numberOfDice;

        // User input for number of dice to roll
        System.out.print("How many dice? ");
        numberOfDice = userInput.next();
        System.out.println("Rolling " + numberOfDice + " dice!");
        userInput.close();

        Integer roll = 0;
        int dice = Integer.parseInt(numberOfDice);
        int sides = 10;   // # of die sides
        ArrayList<Integer> sux = new ArrayList<Integer>();  // Store results of roll


        // print result
        for(int d=0; d < dice; d++) {
          // roll should be 1 through sides
          roll = (int) (Math.random() * sides) + 1;
          sux.add(roll);
        }
        System.out.println(sux);

        // Count successes and print or check for botch 
        for(int s = 0; s < sux.size(); s++){
          if(sux.get(roll) >= 7) {
            s++;
            System.out.println(s + " successes!");
        } else {
            System.out.println("BOTCH!");
            break;
        }
    }
  }
}

Everything after it prints the sux ArrayList is a mess. I know that the for loop is wrong, I just don't know how to make it right. The variable s seems out of place... Any help would be appreciated, and let me know if this post is against standards for the community in anyway. Thanks!

EDIT: To clarify my ramblings, my question is: how to check if the numbers that are being added to the ArrayList after rolling are greater than or equal to 7 (or any number)?


Solution

  • As #Aomine suggestion earlier you need a flag which helps you to find rather if any dice is >=7 or no dice reach this condition.

       // Count successes and print or check for botch 
            boolean isBotch=false;
            for(int s = 0; s < sux.size(); s++){
              if(sux.get(s) >= 7) {
                //s++; //no need to use this counter here again
                System.out.println((s+1) + " successes!"); // s+1 gives you right location
            } else {
                isBotch = true; //flag variable
            }
           }
    
            if(!isBotch){
                System.out.println("BOTCH!");
            }