简体   繁体   中英

Java ArrayList loop issues

I am programming a monopoly-esque game with java on eclipse. I am currently working on a method that allows players to loop through their own squares and choose which one to develop.

for (int loop2 = 0; loop2 < currentPlayer.getOwnedSquares().size(); loop2++) {

    count++;

    System.out.println("Would you like to develop this property " + count + ". " 
    + currentPlayer.getOwnedSquares().get(loop2).getName() + " (y/n)");

    propertyChoice = scanner.nextLine();

    if (propertyChoice.equalsIgnoreCase("Y")) {
            break;
        }else if (propertyChoice.equalsIgnoreCase("N")) {

            continue;
        }
    }
System.out.println("Please choose a development option");
System.out.println("1.Buy a start-up");
System.out.println("2.Buy a global corporation");
int option = scanner.nextInt();

I am unable to get the loop to present only one owned square at a time so the player can choose to select y/n for which one the want to develop. If the player was to pick "N" The loop would then present the next owned property in the array and the player would make another decision and so on.. If the player was to pick "Y" then the loop would break and move on the development options for the chosen owned square.

Any advice on how to realise this would be hugely appreciated.

You have to move the check for user input out of the loop, so the algorithm would look like this:

  1. Print all the owned squares in a loop.
  2. Ask user (outside the loop) which square he wants to develop. For example, a user can simply provide a positional number of a square which you can get by

     currentPlayer.getOwnedSquares().get(Integer.valueOf(userInput)); 
  3. Do whatever you need with selected square.

I just modified the code to test, and it works as you want. I think there is something else problematic which you haven't shared.

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        int count=0;
        String propertyChoice;
        Scanner scanner = new Scanner(System.in);
        for (int loop2 = 0; loop2 < 5; loop2++) {

            count++;

            System.out.println("Would you like to develop this property " + count
                     + " (y/n)");

            propertyChoice = scanner.nextLine();

            if (propertyChoice.equalsIgnoreCase("Y")) {
                break;
            }else if (propertyChoice.equalsIgnoreCase("N")) {

                continue;
            }
        }
        System.out.println("Please choose a development option");
        System.out.println("1.Buy a start-up");
        System.out.println("2.Buy a global corporation");
    }
}

Output:

Would you like to develop this property 1 (y/n)
n
Would you like to develop this property 2 (y/n)
n
Would you like to develop this property 3 (y/n)
y
Please choose a development option
1.Buy a start-up
2.Buy a global corporation

Process finished with exit code 0

Try putting scanner.nextLine(); inmediately before propertyChoice = scanner.nextLine();

Edit: if this doesn't work, notice that the else has no brackets surrounding the second if block. I don't know if this will work as I do not see the classes you are refering to and cannot say there is the error. The code you've shown doesn't seem to have any other issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM