ArrayList如果循环无法正常工作:JAVA

我有一个询问用户名称等的程序,然后询问您要让数字循环多少次(因此我的程序会生成3个介于7和13之间的随机数,如果它加起来等于31,它们就是获胜者),我的问题是,我只希望最后打印的号码在玩家赢或输的情况下计入,其他数字仅用于显示或挑逗。问题在于,无论玩家赢还是输,无论输掉什么,总是会输出失败的陈述。下面是我的整个代码。

 import java.util.InputMismatchException;
 import java.util.Scanner;
 import java.io.IOException;
 import java.util.Random;

 public class StringVariables {



public static void main(String[] args) throws NumberFormatException,
        IOException {

    // user inputs their name in this section
    Scanner user_input = new Scanner(System.in);
 //enter their first name

    String first_name;
    System.out.print("Enter Your First Name: ");
    while
        (!user_input.hasNext("[A-Za-z]+")) {
        System.out.println("Please only enter alphabet characters. Try again.");
        user_input.next();
    }
    first_name = user_input.next();

 //enter their last name
    String last_name;
    System.out.print("Enter Your Last Name: ");
    while
        (!user_input.hasNext("[A-Za-z]+")) {
        System.out.println("Please only enter alphabet characters. Try again.");
        user_input.next();
    }
    last_name = user_input.next();
  //full name printed together
    String full_name;
    full_name = first_name + " " + last_name;

    System.out.println(full_name + " Is Now Playing");

    // this is the shuffle portion as well as something to see if a number

    int numShuffles = -1;
    while (numShuffles < 0) {

        System.out.println("How many times do you want the numbers shuffled? ");

        try {
            numShuffles = user_input.nextInt();
        } catch (InputMismatchException inputException) {
            System.out.print("Please enter a valid number. \n");
            //this is the buffer that resets if the user types a letter instead of a number, or any other character
            user_input.next();
        }
    } 

    // here is going to be the loop for shuffles

    // we are now going to generate their random number and add a delay
    // after completing their name fields

    delay(3000);
    System.out
            .println(" You will be given " + numShuffles +  " hand(s) of 3 random numbers between 7-13" );

    delay(2000);
    System.out
            .println(" Then, the computer will add the random numbers and if it is equal to 31, you win!");

    /*
     * end of explanation of the game, next i will create a new screen with
     * the user's name and numbers
     */

    delay(4000);
    // printing 25 blank lines
    for (int i = 0; i < 25; i++)
        System.out.println(" ");


    System.out.println("User playing: " + full_name);

    System.out.println("Number of times shuffled: " + numShuffles);

    System.out.println("Your lucky numbers are...");

    // random number generator

    Random random = new Random();



    while (true) {

        // the shuffle loop
        Arraylist numberStore = new Arraylist();
        boolean isWinner = false;
        for (int i = 0; i < numShuffles; i++) {
            int num1 = 7 + random.nextInt(7);
            int num2 = 7 + random.nextInt(7);
            int num3 = 7 + random.nextInt(7);

            System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + (num1 + num2 + num3));
            numberStore.add(num1 + num2 + num3);


        int lastNumber = (numberStore.size() - 1);
        if (lastNumber == 31) {
                isWinner = true;
                System.out.println("Congratulations !! You are the Lucky Winner !!!!");
                break;
                //if you loose every shuffle
        }
        }
       if (!isWinner) {
            System.out.println("Better Luck Next Time");
       }
        // play again prompt
        System.out
                .println(" Do you want to play again? (If you do enter y or yes) \n To exit press any other key ");
        String input = user_input.next();
        if (!"y".equalsIgnoreCase(input) && !"yes".equalsIgnoreCase(input)) {
            break;
        }
    }

    // if pressed y or yes the program will run again with the same number of shuffles entered from before
    user_input.close();
}

// delay field

public static void delay(int millis) {
    try {
        Thread.sleep(millis);
    } catch (InterruptedException exp) {

        // delay field

    }
}
 }
死神
                 int lastNumber = (numberStore.size() - 1);
                 if (lastNumber == 31) {

你可能想要类似的东西

                 int lastNumber = numberStore.get(numberStore.size() - 1);
                 if (lastNumber == 31) {

验证这是错误,尝试将该行更改为

                 int lastNumber = num1 + num2 + num3;

根据更多消息进行编辑:

看起来您真正想要的是:

    for (int i = 0; i < numShuffles; i++) {
        int num1 = 7 + random.nextInt(7);
        int num2 = 7 + random.nextInt(7);
        int num3 = 7 + random.nextInt(7);


        System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + (num1 + num2 + num3));
        numberStore.add(num1 + num2 + num3);


        int lastNumber = num1 + num2 + num3;

        boolean lastShuffle = (i == (numShuffles - 1));
        if (lastShuffle) {
            if (lastNumber == 31) {
                System.out.println("Congratulations !! You are the Lucky Winner !!!!");
            } else {
                System.out.println("Better Luck Next Time");
            }
        }
    }
    // play again prompt
    System.out
        .println(" Do you want to play again? (If you do enter y or yes) \n To exit press any other key ");
    String input = user_input.next();
    if (!"y".equalsIgnoreCase(input) && !"yes".equalsIgnoreCase(input)) {
        break;
    }

只是一个一般性建议:尽可能避免使用break,这会使控制流难以遵循,也不是一种好的编程习惯。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章