While “!b.equals(x) || !b.equals(y)” is an infinit

2019-03-05 23:20发布

问题:

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.