Java程序循环不会执行并提示用户?

Java程序循环不会执行并提示用户?,java,arrays,loops,while-loop,prompt,Java,Arrays,Loops,While Loop,Prompt,好的,所以我只是数组清单程序的一部分,但是我的while循环不会执行,该循环用于在用户输入无效数字时提示用户重新输入数字。当输入一个无效的数字时,程序就结束了……我尝试了两种方法,但都不起作用。这就是我现在的处境,有什么想法吗 多谢 public class InventorySystem { public static void main(String[] args) { String[] newItemInfo = new String[1000];

好的,所以我只是数组清单程序的一部分,但是我的while循环不会执行,该循环用于在用户输入无效数字时提示用户重新输入数字。当输入一个无效的数字时,程序就结束了……我尝试了两种方法,但都不起作用。这就是我现在的处境,有什么想法吗

多谢

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);
        }
    }
}
isValid=false时不要执行此操作。你把它设为假的

取而代之的是

while (!isValid) {

}
另外,当isValid==false时不要这样做-这是一段难看的代码

接下来,在循环中更改是有效的

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      
}
否则,您将陷入无限循环。这里要学到的教训是,在头脑中遍历代码,就像在大脑中运行代码一样。通过这种方式,您可以捕捉到与您的线路类似的逻辑错误。

while(isValid = false)
这个=不做你认为它做的事。在Java中,single=表示将右侧的表达式分配给左侧的变量。这并不意味着要比较双方

所以你应该这样写:

while (isValid == false)
请注意double==。这段代码可以工作,但您可以编写得更漂亮:

while (!isValid)

这个!意味着不是。

根据代码,op的代码是无限的loop@KickButtowski字体我知道。这就是为什么我建议他的改变在循环中是有效的。