有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

好的,所以我只是数组清单程序的一部分,但是我的while循环不会执行。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);
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    在你的队伍里

    while(isValid = false)
    

    这个=并不像你想象的那样。在Java中,单个=表示将右侧的表达式赋值给左侧的变量。这并不意味着要比较双方

    所以你应该这样写:

    while (isValid == false)
    

    请注意双==。这段代码很有效,但你可以写得更漂亮:

    while (!isValid)
    

    !表示而不是