Java程序循环不会执行并提示用户吗? [英] Java Program Loop won't Execute and Prompt User?

查看:50
本文介绍了Java程序循环不会执行并提示用户吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我只是阵列清单程序的一部分,但是我的while循环本应该提示用户在输入无效数字时重新输入,但是不会执行。输入无效数字后,程序就结束了……我尝试了几种方法,但都无法正常工作。这就是我现在的位置,有什么想法吗?

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??

非常感谢!

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)时不要执行 。您将其设置为false!

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

相反

while (!isValid) {

}

另外,在(isValid == false)-这是丑陋的代码。

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

接下来,在循环中更改isValid。

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.

这篇关于Java程序循环不会执行并提示用户吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆