While “!b.equals(x) || !b.equals(y)” is an infinite loop?

大兔子大兔子 提交于 2019-12-02 22:10:03

问题


Hey guys this is my code and what it is doing is going in an loop but what its suppose to do is if the user types in borrow then it will ask the user how much which it does but then they type in a number and it will ask them again would you like to borrow or sell and it is in an infinite loop.

case 3:
            do{
                System.out.println("What would you like to do? Please type borrow to borrow money or sell to sell assets: ");
                    b = scan.nextLine().toLowerCase();
                if(b.equals("borrow")){
                    System.out.print("how much would you like to borrow Remmber if you go over 50000 debt its game over.");
                    try {
                        input = scan.nextInt();
                    } catch (Exception e) {
                        System.err.println("That is not a number!!");
                    }
                    account.setdebt(account.getDebt() + input);
                    account.setBalance(account.getBalance() + input);
                    System.out.println("Your new Balance is " + account.getBalance());
                }
                else if(b.equals("sell")){
                    sellA();
                }else{
                    System.out.println("You didn't input 'borrow' or 'sell'. Reinput please");
                }
            }while(!b.equals("borrow") || !b.equals("sell"));
            break;

回答1:


You need to change || to && inside while, otherwise the condition is always true. There'll always be at least one of those two values that b is not equal to.



来源:https://stackoverflow.com/questions/22397105/while-b-equalsx-b-equalsy-is-an-infinite-loop

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