-
Java Arraylist loop problem
about 8 years ago
-
almost 9 years ago
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 } } }
-
-
almost 9 years ago
I see the issue in line no 105
int lastNumber = (numberStore.size() - 31);
Use int lastNumber = (int) numberStore.get(numberStore.size()-1);
Please let me know if it doesn't work.
-
2 Answer(s)