Java Program Loop won't Execute and Prompt User? [closed]

被刻印的时光 ゝ 提交于 2020-01-16 04:22:06

问题


Okay so I am only part of the way through my array inventory program, but my while loop that is supposed to prompt a user to re-enter a number when they have entered an invalid one will not execute. When an invalid number is entered the program just ends...I have tried a couple methods and neither are working. this is where i am at now, any ideas??

thanks much!

public class InventorySystem {

    public static void main(String[] args) {
        String[] newItemInfo = new String[1000];
        String[] itemDescription = new String[1000];
        int[] itemPrice = new int[1000];
        int choice;
        boolean isValid;
        int itemChoice;

        Scanner inputDevice = new Scanner(System.in);
        System.out.println("*****Raider Inventory System*****");
        System.out.println("1. Enter a new item");
        System.out.println("2. View Item Information");
        System.out.println("3. View a list of all items");
        System.out.println("9. End Program\n");
        System.out.println("Please enter your selection: ");
        choice = inputDevice.nextInt();

        if (choice == 1 || choice == 2 || choice == 3 || choice == 9) {
            isValid = true;
        } else {
            isValid = false;
        }

**      while (isValid = false) {
            System.out.println("Invalid entry, please enter either 1, 2, 3, or 9 for menu options.");
**      }

        for (int x = 0; x < 1000; ++x) {
            if (choice == 1) {
                System.out.println("Please enter the name if the item: ");
                newItemInfo[x] = inputDevice.nextLine();
                System.out.println("Please enter the item description: ");
                itemDescription[x] = inputDevice.nextLine();
                System.out.println("Please enter item price: ");
                itemPrice[x] = inputDevice.nextInt();
            }
        }

        if (choice == 2) {
            System.out.println("***Show item Description***");
            System.out.println("0. ");
            System.out.println("please enter the item number ot view details: ");
            itemChoice = inputDevice.nextInt();
        }

        if (choice == 3) {
            System.out.println("****Item List Report****");
        }

        if (choice == 9) {
            System.exit(0);
        }
    }
}

回答1:


Don't do while (isValid = false). You're setting it to false!

Instead do

while (!isValid) {

}

Also, don't do while (isValid == false) -- that's ugly code.

Next, change isValid inside the loop.

while (!isValid) {

   // get input from user in here

   // test input and check if is valid. If so, set isValid = true;

   // something must set isValid to true in here, else it will 
   // loop forever      
}

Otherwise you'll be stuck in an infinite loop. The lesson to be learned here is to mentally walk through your code as if you were running it in your brain. This way you catch logic errors like what you've got.




回答2:


In your line

while(isValid = false)

the = doesn’t do what you think it does. In Java, a single = means assign the expression on the right side to the variable on the left side. It does not mean to compare the two sides.

So you should rather write this:

while (isValid == false)

Note the double ==. This code works, but you can write it even more beautifully:

while (!isValid)

The ! means not.



来源:https://stackoverflow.com/questions/27196955/java-program-loop-wont-execute-and-prompt-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!