Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java Arraylist loop problem

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 653
    Answer it
    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
            Arraylist numberStore = new Arraylist();
            Random random = new Random();
    
            while (true) {
    
                // the shuffle loop
            	
                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() - 31);
                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
    
            }
        }
    }

    hello! this is my first post to this site. im new to java and have a question. i have a program that asks the user their name etc. then it asks how many times do you want the numbers to loop (so my program is that it generates 3 random numbers between 7 and 13 and if it adds up to 31 they are the winner) and my issue is that i only want the last printed number to count towards if the player wins or looses, the other numbers are just for show or tease i guess. the problem is that regardless towards if the player wins or looses, the losing statement always prints out. above is my entire code. if anyone can help it would be such an honour.

     

     

     

 2 Answer(s)

  • Hi
    I have resolved your issue, please test below code :

        import java.util.ArrayList;
    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 userinput = new Scanner(System.in);
        //enter their first name
    
                String firstname;
                System.out.print("Enter Your First Name: ");
                while
                    (!userinput.hasNext("[A-Za-z]+")) {
                    System.out.println("Please only enter alphabet characters. Try again.");
                    userinput.next();
                }
                firstname = userinput.next();
    
        //enter their last name
                String lastname;
                System.out.print("Enter Your Last Name: ");
                while
                    (!userinput.hasNext("[A-Za-z]+")) {
                    System.out.println("Please only enter alphabet characters. Try again.");
                    userinput.next();
                }
                lastname = userinput.next();
        //full name printed together
                String fullname;
                fullname = firstname + " " + lastname;
                System.out.println(fullname + " 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 = userinput.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
                        userinput.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: " + fullname);
    
                System.out.println("Number of times shuffled: " + numShuffles);
                System.out.println("Your lucky numbers are...");
                // random number generator
                ArrayList<Integer> numberStore = new ArrayList<Integer>();
                Random random = new Random();
                while (true) {
                    // the shuffle loop
    
                    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 = (int) numberStore.get(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 = userinput.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
                userinput.close();
            }
    
            // delay field
            public static void delay(int millis) {
                try {
                    Thread.sleep(millis);
                } catch (InterruptedException exp) {
                    // delay field
                }
            }
        }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: