检查存储在 ArrayList 中的值是否大于数字

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

学习基础知识后,我正在尝试 Java 中的第一个项目,因此对于这可能是一个完整的初学者问题,我深表歉意。我整个下午都在寻找帮助,但我的代码开始变得凌乱和损坏。

我正在尝试创建一个掷骰子的掷骰子,它掷出多个 d10,然后继续检查有多少是 7 或以上,并且 count/display 它们是成功的。如果 none 的掷骰成功,我希望它显示 "Botch"。

经过一些研究,我发现创建一个 ArrayList 并从那里开始(在我看来)是最好的选择。但是我现在已经被困了一段时间,出现了不同的错误和问题,每次我拿下一个,代码变得更加混乱,我的目标似乎更远。我哥们说"welcome to programming"建议我问社区

这是我正在处理的内容:

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

打印 sux ArrayList 后的一切都是一团糟。我知道 for 循环是错误的,我只是不知道如何让它正确。变量 s 似乎不合适...任何帮助将不胜感激,如果这个 post 无论如何都违反社区标准,请告诉我。谢谢!

编辑:为了澄清我的胡言乱语,我的问题是:如何检查滚动后添加到 ArrayList 的数字是否大于或等于 7(或任何数字)?

更新:

for(int s = 0; s < sux.size(); s++){
          if(sux.get(roll) >= 7) { //here value of sux.get(roll) will be regardless to s. should be typo.
            s++;// no need to change value of s, as it will be changed during for clause.
            System.out.println(s + " successes!");

            break;
        } else {
            System.out.println("BOTCH!");
        }

应该是

bool foundvalue = false;  
    for(int s = 0; s < sux.size(); s++){
          if(sux.get(s) >= 7) {
            System.out.println((s+1) + " successes!");
            foundvalue = true;
            break;
        } 
     }
     if (!foundvalue){
            System.out.println("BOTCH!");
        }

这个怎么样?

    System.out.println(sux);

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

    if(gtNumber == 0){
        System.out.println("BOTCH!");
    }

}

正如 #Aomine 之前的建议,您需要一个标志来帮助您找到是否有任何骰子 >=7 或没有骰子达到此条件。

   // 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!");
        }